How to use a regular expression for finding decimal or float numbers with JavaScript?

Sometimes, we want to use a regular expression for finding decimal or float numbers with JavaScript.

In this article, we’ll look at how to use a regular expression for finding decimal or float numbers with JavaScript.

How to use a regular expression for finding decimal or float numbers with JavaScript?

To use a regular expression for finding decimal or float numbers with JavaScript, we use a regex that matches digits, signs and decimal points.

For instance, we write

/^[+-]?d+(.d+)?$

to write a regex that matches the plus or minus sign at the beginning of the string with [+-].

We match digits that comes after it with d+.

And we match the fractional part with (.d+)?.

Conclusion

To use a regular expression for finding decimal or float numbers with JavaScript, we use a regex that matches digits, signs and decimal points.