EN
JavaScript - remove leading commas using regular expression
3 points
In this article, we would like to show you how to remove leading commas from string using regular expression in JavaScript.
In this example, we use replace()
method with /^,+/
regular expression to remove leading commas from the text
.
xxxxxxxxxx
1
var text = ',,,a,b,c';
2
var expression = /^,+/;
3
4
text = text.replace(expression, '');
5
6
console.log(text); // 'a,b,c'
Regular expression explanation:
^
- matches the beginning of the string,,
- matches comma character,+
- matches one or more of the preceding token (in our case one or more,
).