SoFunction
Updated on 2025-03-01

js method to remove the first comma of a string

Copy the codeThe code is as follows:

<script type="text/javascript">
var s=',dddd';
if ((0,1)==',')
s=(1);
(s);
</script>


Here are some better codes:

Copy the codeThe code is as follows:

<script language="javascript"> 
var str="asdfk,asdf345345,345345";
//Replace all characters except numbers and commas.
str=(/[^0-9,]*/g,"");
// Remove the first comma
if ((0,1)==',') str=(1);
// Remove the last comma
var reg=/,$/gi;
str=(reg,"");
alert(str);
</script>


The following mainly adds the method of removing multiple commas into one comma

Copy the codeThe code is as follows:

function dostr(str){
str=trim(str);
var strarry=unique((","));
str=(",");
str=(/,/ig,",");
str=(/[^0-9,]*/ig,"");
str=(new RegExp(',+',"gm"),',');
if ((0,1)==',') str=(1);
var reg=/,$/gi;
str=(reg,"");
return str;
}