如何在欧芹数据欧芹模式中转义双引号?

How to escape double quote in parsley data-parsley-pattern?

我正在使用 parsley 进行 javascript 验证。我当前的正则表达式模式是

data-parsley-pattern="/^[0-9a-zA-Z\!\@\#$\%\^\&\*\(\)\-\_\+\?\'\.\,\/\r\n ]+$/"

如何在我的模式中添加 双引号。我已将 \" 添加到模式

data-parsley-pattern="/^[0-9a-zA-Z\!\@\#$\%\^\&\*\(\)\-\_\+\?\'\"\.\,\/\r\n ]+$/"

但它不起作用。

请注意,您对模式进行了过度转义,几乎所有您转义的字符都不是字符中的特殊字符 class。

接下来,如果您使用字符串模式,您可以缩短代码。见 Parseley docs:

data-parsley-pattern="\d+"

Note that patterns are anchored, i.e. must match the whole string. Parsley deviates from the standard for patterns looking like /pattern/{flag}; these are interpreted as literal regexp and are not anchored.

这意味着如果您定义没有正则表达式定界符的模式,则不需要 ^$/.

关于引号,你可以使用常见的\xXX符号。

您可以使用

data-parsley-pattern="[0-9a-zA-Z!@#$%^&*()_+?\x27\x22.,/\r\n` -]+"

data-parsley-pattern="/^[0-9a-zA-Z!@#$%^&*()_+?\x27\x22.,/\r\n` -]+/$"

其中 \x27'\x22"

请注意字符 class 末尾的 - 是文字连字符的安全位置,您不必将其转义。