使用正则表达式允许字母字符、连字符、下划线、空格和数字

Muh*_*mad 4 validation laravel

我想在特殊情况下验证使用 Laravel。我授权的字段是一本书的名字。因此它可以包含字母字符、数字字符、空格和连字符/下划线/任何其他键。我唯一不希望它在输入任何键之前的开头有空格。所以名称不能是“L”,注意空格,而“LL L”是完全可以接受的。在这种情况下有人可以帮助我吗?

到目前为止,我得到了这样的正则表达式验证:

regex:[a-z{1}[A-Z]{1}[0-9]{1}]
Run Code Online (Sandbox Code Playgroud)

我不确定如何包含其他限制。

Mar*_*boc 7

    \n
  • 简短回答:
  • \n
\n

对于带有空格的 alpha_num 使用此正则表达式:

\n
'regex:/^[\\s\\w-]*$/'\n
Run Code Online (Sandbox Code Playgroud)\n
    \n
  • 长一点:)
  • \n
\n

这是 regEx 的一些定义块:

\n
^           ==>  The circumflex symbol marks the beginning of a pattern, although in some cases it can be omitted\n$           ==>  Same as with the circumflex symbol, the dollar sign marks the end of a search pattern\n.           ==>  The period matches any single character\n?           ==>  It will match the preceding pattern zero or one times\n+           ==>  It will match the preceding pattern one or more times\n*           ==>  It will match the preceding pattern zero or more times\n|           ==>  Boolean OR\n\xe2\x80\x93           ==>  Matches a range of elements\n()          ==>  Groups a different pattern elements together\n[]          ==>  Matches any single character between the square brackets\n{min, max}  ==>  It is used to match exact character counts\n\\d          ==>  Matches any single digit\n\\D          ==>  Matches any single non digit character\n\\w          ==>  Matches any alpha numeric character including underscore (_)\n\\W          ==>  Matches any non alpha numeric character excluding the underscore character\n\\s          ==>  Matches whitespace character\n
Run Code Online (Sandbox Code Playgroud)\n

如果您想添加一些其他字符,您应该做的就是将其添加到[]块中。

\n

例如,如果您想允许,==> 'regex:/^[\\s\\w-,]*$/'

\n

PS:如果您想要一个像 \\ * 或这样的 setial char,还有一件事。你必须像这样转义 \\ * 。

\n

对于*==>'regex:/^[\\s\\w-,\\*]*$/'

\n