I'm looking for a regular expression in JavaScript that tests whether a string is a number with one or two digits before the decimal point and optionally a maximum of five digits after decimal point.
Examples of correct values:
- 12.345
- 12.12
- 1.3
- 1.12345
- 12
What would be the correct regex for this?
Regex
Demo
Explanation
/ /
: the beginning and end of the expression^
: whatever follows should be at the beginning of the string you're testing\d{1,2}
: there should be one or two digits here( )?
: this part is optional\.
: here goes a dot\d{1,5}
: there should be between one and five digits here$
: whatever precedes this should be at the end of the string you're testingTip
You can use regexr.com or regex101.com for testing regular expressions directly in the browser!