且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

仅验证Laravel中的字母数字字符

更新时间:2023-02-21 11:46:31

您需要确保模式与整个输入字符串匹配.另外,字母数字和下划线符号可以与\w匹配,因此正则表达式本身可以大大缩短.

You need to make sure the pattern matches the whole input string. Also, the alphanumeric and an underscore symbols can be matched with \w, so the regex itself can be considerably shortened.

我建议:

'regex:/^[\w-]*$/'

详细信息:

  • ^-字符串开头
  • [\w-]*-零个或多个来自[a-zA-Z0-9_]范围或- s
  • 的单词字符
  • $-字符串结尾.
  • ^ - start of string
  • [\w-]* - zero or more word chars from the [a-zA-Z0-9_] range or -s
  • $ - end of string.

为什么比'alpha_dash' 好:您可以进一步自定义此模式.

Why is it better than 'alpha_dash': you can further customize this pattern.