Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
你能帮我用一个允许几种有效格式的字符串的正则表达式吗?
格式为:
5 位破折号 6 位
5 位破折号 7 位
6 位破折号 6 位
7digits 破折号 7digits
7位破折号 8位
我问这个是因为我对正则表达式几乎一无所知。
谢谢。
我建议您对这个正则表达式保持简单:
/^\d{5,7}-\d{6,8}$/
即使您指定的格式可以通过以下正则表达式进行验证:
^\d{5}-\d{6,7}|\d{7}-\d{7,8}|\d{6}-\d{6}$
但这对于未来的变化来说不是很容易维护。