Categories
JavaScript Answers

How to use regular expression for formatting numbers with thousands separators in JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *