我在创建正则表达式匹配URL slugs时遇到了麻烦(基本上,单个破折号分隔的字母数字"单词" )
this-is-an-example
我想出了这个正则表达式:/[a-z0-9\-]+$/虽然它将字符串限制为只有字母数字字符和短划线,但它仍会产生一些误报,如下所示:
-example
example-
this-----is---an--example
-
我对正则表达式很不好,所以任何帮助都会受到赞赏.
Roh*_*ain 45
你可以用这个:
/^
  [a-z0-9]+   # One or more repetition of given characters
  (?:         # A non-capture group.
    -           # A hyphen
    [a-z0-9]+   # One or more repetition of given characters
  )*          # Zero or more repetition of previous group
 $/ 
这将匹配: