简体   繁体   English

正则表达式允许字符串、字符串后跟数字和连字符

[英]regex to allow string, string followed by numbers, and hyphen

I wanna regex which will allow the following samples我想要正则表达式,它将允许以下示例

1) abcd
2) abcd123
3) abcd-123

which should not allow这不应该允许

1) 123abcd
2) 123
3) 123-123

You can use this regex:您可以使用此正则表达式:

/^[a-z]+-?\d*$/i

RegEx Breakup:正则表达式分解:

  • ^ - Assert start ^ - 断言开始
  • [az]+ - Match 1 or more alphabets [az]+ - 匹配 1 个或多个字母
  • -? - Match an optional hyphen - 匹配一个可选的连字符
  • \\d* - Match 0 or more digits \\d* - 匹配 0 个或多个数字
  • $ - Assert end $ - 断言结束

我认为,如果小写

[a-z]-?\d

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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