正则表达式字母数字 + 空格 + PHP 中的 3 个字符,如果可能的话,JavaScript


regex alphanumeric + space + 3 chars in php and if possible javascript

我需要找到好的正则表达式来使用 PHP 和 JavaScript,接受的字符是:

  é  è ' space and alphanumeric

我试过这个:

if(!preg_match("/^[a-z0-9]+([''s]{1}[a-z0-9]|[a-z0-9])+$/i", $name))

如果你有php和javascript的解决方案,它将是一个完美的世界:)

感谢您的帮助

所以你应该使用相应的正则表达式:

^[[:alnum:]éè ]+$

解释:

^                      # start of string
    [                  # match these characters
        [:alnum:]      # all alphanumeric chars
        é              # é letter
        è              # è letter
                       # a space
    ]                  # end of char group
    +                  # one or more sequence of these characters
$                      # end of string

现场演示

使用 unicode 属性:

preg_match("/^['pL'pN's']+$/u", $name)

'pL代表任何字母
'pN代表任何数字

您的正则表达式可能会被提炼为

 #  ^[a-zA-Z0-9éè'](?:[ ]?[a-zA-Z0-9éè']+)*$     
 ^ 
 [a-zA-Z0-9éè'] 
 (?: [ ]? [a-zA-Z0-9éè']+ )*
 $