Js method to remove the first comma from a string
- 2020-03-30 03:15:55
- OfStack
<script type="text/javascript">
var s=',dddd';
if (s.substr(0,1)==',')
s=s.substr(1);
document.write(s);
</script>
Here's some better code:
<script language="javascript">
var str="asdfk,asdf345345,345345";
//Replace all characters except Numbers and commas.
str=str.replace(/[^0-9,]*/g,"");
//Remove the first comma
if (str.substr(0,1)==',') str=str.substr(1);
//Remove the last comma
var reg=/,$/gi;
str=str.replace(reg,"");
alert(str);
</script>
The following is mainly to add the method of removing multiple commas into one comma
function dostr(str){
str=trim(str);
var strarry=unique(str.split(","));
str=strarry.join(",");
str=str.replace(/ . /ig,",");
str=str.replace(/[^0-9,]*/ig,"");
str=str.replace(new RegExp(',+',"gm"),',');
if (str.substr(0,1)==',') str=str.substr(1);
var reg=/,$/gi;
str=str.replace(reg,"");
return str;
}