Reputation: 487
I am trying to create a regular expression to accept
an integer
123
or
an integer then underscore then another integer
123_45
Here is what I have
/^[0-9]+_*[0-9]*$/
how do i make _*[0-9]*
(the second part) optional
Upvotes: 2
Views: 87
Reputation: 3773
Have you tried
/^[0-9]+(_[0-9]+)?$/
Optional syntax ()?
taken from http://www.regular-expressions.info/optional.html
But as stated in the accepted answer \d
can be used as shorthand for [0-9]
so this could be
/^\d+(_\d+)?$/
Upvotes: 2