简体   繁体   中英

Make this regular expression shorter

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM