Logo Questions Linux Laravel Mysql Ubuntu Git Menu

Version number validation

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 ?

like image 832
Mahesh Avatar asked Dec 26 '22 12:12


1 Answers

The following regex should work:


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.

like image 170
PlasmaPower Avatar answered Jan 12 '23 07:01