allow parentheses and other symbols in regex

前端 未结 5 1003
醉话见心
醉话见心 2020-12-15 14:18

I\'ve made this regex:

^[a-zA-Z0-9_.-]*$

Supports:

letters [uppercase and lowercase]
numbers [from 0 to 9]
underscores [_]
         


        
5条回答
  •  抹茶落季
    2020-12-15 14:35

    The hyphen is being treated as a range marker -- when it sees ,-! it thinks you're asking for a range all characters in the charset that fall between , and ! (ie the same way that A-Z works. This isn't what you want.

    Either make sure the hyphen is the last character in the character class, as it was before, or escape it with a backslash.

    I would also point out that the quote characters you're using “”„ are part of an extended charset, and are not the same as the basic ASCII quotes "'. You may want to include both sets in your pattern. If you do need to include the non-ASCII characters in the pattern, you should also add the u modifier after the end of your pattern so it correctly picks up unicode characters.

提交回复
热议问题