How to match exact word anywhere in string with PHP regexp

后端 未结 4 680
醉梦人生
醉梦人生 2021-01-27 11:15

I want to find out if user has used the words admin or username anywhere in their possible username string.

So if user wants to use admin

4条回答
  •  不知归路
    2021-01-27 11:58

    Square brackets in a regexp are not for grouping, they're for specifying character classes; grouping is done with parentheses. You don't want to anchor the regexp with ^ and $, because that will only match at the beginning and end of the string; you want to use \b to match word boundaries.

    /\b(admin|username)\b/i
    

提交回复
热议问题