我想屏蔽以下字符:
(
)
/
>
<
]
[
\
"
,
;
|
我的模式有什么问题?
pattern="[^()/<>[]\,'|\x22]+"
最佳答案
What is wrong with my pattern?
它包含一个否定字符类,您无法在其中转义 ]
象征。 在字符类中,它*必须在 JS 中转义,在字符类之外,你不必转义它。因此,您的 "[^()/<>[]\,'|\x22]+"
pattern 实际上意味着:匹配一个 而且只有一个! 字符而不是 ()/<>[
(带有 [^()/<>[]
部分)后跟一个 *sequence of ,'
, -OR- (因为 |
是一个交替运算符,如果没有在字符类之外转义)一个双引号(与 \x22
匹配)后跟一个或多个文字 ]
符号(因为 ]
匹配字符类之外的文字 ]
,而 +
是为其设置的量词。
你真的需要
pattern="[^()/><\][\\\x22,;|]+"
<form action="#" method="post">
<input name="name1" pattern="[^()/><\][\\\x22,;|]+" placeholder="New value" title="No special characters!">
<input class="submit" value="Submit!" type="submit" name="name2">
</form>
在一个字符类中,除了]
, 你需要转义 \
象征。还有一个 -
如果它位于两个文字符号之间,但此处没有连字符。
备注:HTML5 pattern attribute默认情况下锚定值,即整个模式被包装到 ^(?:
中和 )$
.您不必输入 ^
和 $
在图案之间。
The regular expression language used for this attribute is the same as that used in JavaScript, except that the pattern attribute is matched against the entire value, not just any subset (somewhat as if it implied a
^(?:
at the start of the pattern and a)$
at the end).
关于regex - HTML5 模式 - 不允许以下字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35065259/