我需要创建一个正则表达式,其结果与可选的(数字或特定代码)和可选的“-”以及另一个不为空的可选(数字或特定代码)匹配。
这是我拥有的正则表达式并且它有效,但我想简化它:
/((\d+|xs|sm|md|lg|xl)|((\d+|xs|sm|md|lg|xl)-)|((\d+|xs|sm|md|lg|xl)-(\d+|xs|sm|md|lg|xl))|(-)|(-(\d+|xs|sm|md|lg|xl)))/
/* these should all work */
"34-56"
"-56"
"34"
"34-"
"-"
"xs-md"
"-lg"
"sm"
"md-"
/* these should not work */
"test"
"500px"
"16-max"
""
测试正则表达式:https : //regex101.com/r/Uz2fGE/2
试试这个!
((\d*|xs|sm|md|lg|xl|-){1}-?(\d*|xs|sm|md|lg|xl))?
参考:https : //developer.mozilla.org/ko/docs/Web/JavaScript/Guide/%EC%A0%95%EA%B7%9C%EC%8B%9D
*: >= 0
?: 0 or 1
(): pattern
\d: number
|: or
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句