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.
Practical example
In this example, we use replace() method with /^,+/ regular expression to remove leading commas from the text.
// ONLINE-RUNNER:browser;
var text = ',,,a,b,c';
var expression = /^,+/;
text = text.replace(expression, '');
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,).