My product version number is of the format "P.Q.R", where P, Q, R are digits. The valid inputs are "P", "P.Q", "P.Q.R".
I wrote regular expression performing an OR
operation.
(^\d+$) | (^\d+.\d+$) | (^\d+.\d+.\d$)
Is there much simpler way to write using JavaScript ?
The following regex should work:
^\d+(\.\d+){0,2}$
The \d+
indicates any number of digits. The (\.\d+)
indicates a dot followed by any number of digits, and the {0,2}
means the last group can be repeated 0-2 times. The ^
and $
indicate the start and end of the string, so the regex will match the whole thing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With