Javascript 正则表达式:允许除某些选定字符之外的所有内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12097651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:06:24  来源:igfitidea点击:

Regex: allow everything but some selected characters

javascriptjqueryregextextarea

提问by Baylock

I would like to validate a textarea and I just don't get regex (It took me the day and a bunch of tutorials to figure it out).

我想验证一个文本区域,但我没有得到正则表达式(我花了一天的时间和一堆教程才弄明白)。

Basically I would like to be able to allow everything (line breaks and chariots included), but the characters that could be malicious( those which would lead to a security breach). As there are very few characters that are not allowed, I assume that it would make more sense to create a black list than a white one.

基本上我希望能够允许所有内容(包括换行符和战车),但可能是恶意的字符(会导致安全漏洞的字符)。由于不允许使用的字符很少,我认为创建黑名单比创建白名单更有意义。

My question is: what is the standard "everything but" in Regex?

我的问题是:Regex 中的标准“除了”是什么?

I'm using javascript and jquery.

我正在使用 javascript 和 jquery。

I tried this but it doesn't work (it's awful, I know..):

我试过这个,但它不起作用(这很糟糕,我知道..):

var messageReg = /^[a-zA-Z0-9éèê?ùüàa???\"\/\%\(\).'?!,@$#§-_ \n\r]+$/;

Thank you.

谢谢你。

采纳答案by Juan Mendes

As Esailija mentioned, this won't do anything for real security.

正如 Esilija 所提到的,这对真正的安全没有任何作用。

The code you mentioned is almost a negated set, as murgatroid99 mentioned, the ^goes inside the brackets. So the regular expression will match anything that is not in that list. But it looks like you really want to strip out those characters, so your regexp doesn't need to be negated.

你提到的代码几乎是一个否定集,正如 murgatroid99 提到的,^在括号内。因此正则表达式将匹配不在该列表中的任何内容。但看起来你真的想去掉这些字符,所以你的正则表达式不需要被否定。

Your code should look like:

您的代码应如下所示:

str.replace(/[a-zA-Z0-9éèê?ùüàa???\"\/\%\(\).'?!,@$#-_ \n\r]/g, "");

That says, remove all the characters in my regular expression.

也就是说,删除我的正则表达式中的所有字符。

However, that is saying you don't want to keep a-zA-Z0-9are you sure you want to strip those out?

但是,这就是说您不想保留a-zA-Z0-9您确定要删除它们吗?

Also, chrome doesn't like § in Regular Expressions, you have to use the \xalong with the hex code for the character

此外,chrome 不喜欢正则表达式中的 §,您必须将\x与字符的十六进制代码一起使用

回答by murgatroid99

If you want to exclude a set of characters (some punctuation characters, for example) you would use the ^operator at the beginning of a character set, in a regex like

如果要排除一组字符(例如某些标点符号),您可以^在字符集的开头使用运算符,在正则表达式中

/[^.?!]/

This matches any character that is not ., ?, or !.

这匹配任何不是., ?, 或 的字符!

回答by Krycke

You can use the ^as the first character inside brackets []to negate what's in it:

您可以使用^括号内的第一个字符[]来否定其中的内容:

/^[^abc]*$/

This means: "from start to finish, no a, b, or c."

这意味着:“从开始到结束,没有abc”。