Can't escape the backslash with regex?

前端 未结 6 671
别那么骄傲
别那么骄傲 2020-11-22 09:04

I\'m using the following regex

^[a-zA-Z0-9\\\',!;\\?\\$\\^:\\\\\\/`\\|~&\\\" @#%\\*\\{}\\(\\)_\\+\\.\\s=-]{1,1000}$

I know it\'s ugly,

6条回答
  •  长发绾君心
    2020-11-22 09:55

    If it's not a literal, you have to use \\\\ so that you get \\ which means an escaped backslash.

    That's because there are two representations. In the string representation of your regex, you have "\\\\", Which is what gets sent to the parser. The parser will see \\ which it interprets as a valid escaped-backslash (which matches a single backslash).

提交回复
热议问题