简体   繁体   English

什么 JavaScript RegEx 允许除某些特殊字符之外的字母数字字符?

[英]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.

相关问题 JavaScript RegEx仅允许AlphaNumeric字符和一些特殊字符 - JavaScript RegEx to allow only AlphaNumeric Characters and some special characters 正则表达式,用于字母数字和某些特殊字符 - RegEx for alphanumeric and some certain special characters 正则表达式允许使用字母数字,空格,一些特殊字符 - Regex to allow alphanumeric, spaces, some special characters Javascript 正则表达式检查准确的字母数字和特殊字符 - Javascript regex to check for exact alphanumeric and special characters REGEX(javascript)-允许带有特殊字符的字母数字字符不在首位 - REGEX (javascript) - Allow alphanumeric characters with special characters not in the first position 如何为字母数字和一些特殊字符制作正则表达式? - How to make regex expression for alphanumeric and some special characters? 不允许使用特殊字符,但允许的字符除外javascript regex - Do not allow special characters except the allowed characters javascript regex javascript正则表达式用空格验证字母数字文本并拒绝特殊字符 - javascript regex to validate alphanumeric text with spaces and reject special characters 用于允许字母数字、特殊字符且不以 @ 或 _ 或结尾的正则表达式 - regex for allowing alphanumeric, special characters and not ending with @ or _ or 正则表达式删除括号内的字母数字和特殊字符 - RegEx to delete alphanumeric and special characters inside a parenthesis
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM