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+)?$/
.