Currently I write the regex like this: /^([\d+]*-)+([\d]*-)+([\d])*$/
I want the result follow this pattern 00-123-456-789
or 00123456789
(dash between number group or no dash at all)
00-123--457-789
-00-123-456-789-
-00123456789-
00-123-456-789-
How can I modify the regex to matches the pattern above?
The quantifier notations In regular expressions, the hyphen ("-") notation has special meaning; it indicates a range that would match any number from 0 to 9. As a result, you must escape the "-" character with a forward slash ("\") when matching the literal hyphens in a social security number.
$ means "Match the end of the string" (the position after the last character in the string).
Definition and Usage. The [0-9] expression is used to find any character between the brackets. The digits inside the brackets can be any numbers or span of numbers from 0 to 9. Tip: Use the [^0-9] expression to find any character that is NOT a digit.
6 Answers. Show activity on this post. [] denotes a character class. () denotes a capturing group.
Try something like this
/^(\d+-?)+\d+$/
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