I was wondering whether it is possible to simplify a regular expression like this one
/^[0-9]{2}[ .-]?[0-9]{2}[ .-]?[0-9]{2}[ .-]?[0-9]{2}[ .-]?[0-9]{2}$/
into something shorter
something like
/^([([0-9]{2})([ .-]?))]{4})[0-9]$/
but I don't seem to get it right, nor even getting close to figuring it out.
This regex:
/^[0-9]{2}[ .-]?[0-9]{2}[ .-]?[0-9]{2}[ .-]?[0-9]{2}[ .-]?[0-9]{2}$/
can be shortened to:
/^(?:[0-9]{2}[ .-]?){4}[0-9]{2}$/
Check out this:
^([0-9]{2}[ .-]?){5}$
It's a lot shorter but will also match 11-11-11-11-11-
. The exact equivalent is posted by @anubhava.
You original regex looks like this:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.