1

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.

4

2 回答 2

4

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}$/
于 2013-11-13T13:22:23.817 回答
2

Check out this:

^([0-9]{2}[ .-]?){5}$

Regular expression visualization

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: Regular expression visualization

于 2013-11-13T13:24:05.633 回答