[英]What JavaScript RegEx allows alphanumeric characters except some special characters?
I need a JavaScript regular expression that allows all alphanumeric characters and rejects "&" (ampersand), ";"我需要一个 JavaScript 正则表达式,它允许所有字母数字字符并拒绝“&”(与号)、“;” (semicolon), and " ' " (apostrophe) .
(分号)和“'”(撇号) 。
I know how to allow only certain characters, such as ^[a-zA-Z0-9 @$]*$
—this allows all alphanumeric characters, spaces, "@" (at-sign), and "$" (dollar-sign).我知道如何只允许某些字符,例如
^[a-zA-Z0-9 @$]*$
— 这允许所有字母数字字符、空格、“@”(at 符号)和“$”(美元-符号)。
How could I tell it to allow all special characters except "&", ";"我怎么能告诉它允许除“&”、“;”之外的所有特殊字符and " ' " , while allowing all alphanumeric ones?
和 " ' " ,同时允许所有字母数字的?
If the whole purpose is to allow everything except for the &
, ;
如果整个目的是允许除
&
之外的所有内容, ;
, and '
symbols, you could just ignore those: , 和
'
符号,你可以忽略那些:
[^&;']*
This should allow all alpha-numeric symbols and every other special character.这应该允许所有字母数字符号和所有其他特殊字符。
If your list of acceptable/rejected characters is more complicated, it may be easier to write a regex specifying what's allowed , like your original sample of ^[a-zA-Z0-9 @$]*$
.如果您的可接受/拒绝字符列表更复杂,则编写一个正则表达式来指定允许的内容可能会更容易,例如
^[a-zA-Z0-9 @$]*$
原始样本。 It all depends on what your needs are and/or if they'll change (I, personally, would prefer your original regex if it's not going to change much).这完全取决于您的需求和/或它们是否会改变(我个人更喜欢您的原始正则表达式,如果它不会有太大变化)。
The format [^ ]
is the opposite of [ ]
.格式
[^ ]
与[ ]
相反。 So to do what you want, try this:所以要做你想做的,试试这个:
^[a-zA-Z0-9 @$]*|[^&;\`]*
/^[a-zA-Z0-9 @$] |[^&;`,"'%!.()] $/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.