Sometimes, we want to use regular expression for formatting numbers with thousands separators in JavaScript.
In this article, we’ll look at how to use regular expression for formatting numbers with thousands separators in JavaScript.
How to use regular expression for formatting numbers with thousands separators in JavaScript?
To use regular expression for formatting numbers with thousands separators in JavaScript, we use the string replace
method.
For instance, we write
const result = subject.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
to replace every groups of 3 digits with the groups plus a comma after it.
Conclusion
To use regular expression for formatting numbers with thousands separators in JavaScript, we use the string replace
method.