Categories
JavaScript Answers

How to write a JavaScript regex for number with decimals and thousand separator?

Spread the love

Sometimes, we want to write a JavaScript regex for number with decimals and thousand separator.

In this article, we’ll look at how to write a JavaScript regex for number with decimals and thousand separator.

How to write a JavaScript regex for number with decimals and thousand separator?

To write a JavaScript regex for number with decimals and thousand separator, we can use /^\d{1,3}(,\d{3})*(\.\d+)?$/.

^\d{1,3} matches groups of digits up to 3 digits long at the start of the string.

We match the digit groups in the middle of the string with (,\d{3})*.

And we match the digit group at the end of the string with (\.\d+)?$.

Conclusion

To write a JavaScript regex for number with decimals and thousand separator, we can use /^\d{1,3}(,\d{3})*(\.\d+)?$/.

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 *