Javascript 字符串替换为正则表达式以去除非法字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3780696/
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 06:07:53  来源:igfitidea点击:

Javascript string replace with regex to strip off illegal characters

javascriptregexstring

提问by JohnIdol

Need a function to strip off a set of illegal character in javascript: |&;$%@"<>()+,

需要一个函数来去除 javascript 中的一组非法字符: |&;$%@"<>()+,

This is a classic problem to be solved with regexes, which means now I have 2 problems.

这是一个用正则表达式解决的经典问题,这意味着现在我有 2个问题

This is what I've got so far:

这是我到目前为止所得到的:

var cleanString = dirtyString.replace(/\|&;$%@"<>\(\)\+,/g, "");

I am escaping the regex special chars with a backslash but I am having a hard time trying to understand what's going on.

我正在用反斜杠转义正则表达式特殊字符,但我很难理解发生了什么。

If I try with single literals in isolation most of themseem to work, but once I put them together in the same regex depending on the order the replace is broken.

如果我单独尝试使用单个文字,它们中的大多数似乎都可以工作,但是一旦我根据顺序将它们放在同一个正则表达式中,替换就会被破坏。

i.e. this won't work --> dirtyString.replace(/\|<>/g, ""):

即这行不通 --> dirtyString.replace(/\|<>/g, ""):

Help appreciated!

帮助表示赞赏!

回答by Lekensteyn

What you need are character classes. In that, you've only to worry about the ], \and -characters (and ^if you're placing it straight after the beginning of the character class "[" ).

你需要的是字符类。在这里,您只需要担心],\-字符(^如果您将它直接放在字符类 " ["的开头之后)。

Syntax: [characters]where charactersis a list with characters.

语法:[characters]其中characters是包含字符的列表。

Example:

例子:

var cleanString = dirtyString.replace(/[|&;$%@"<>()+,]/g, "");

回答by John Culviner

I tend to look at it from the inverse perspective which may be what you intended:

我倾向于从相反的角度来看待它,这可能是你想要的:

What characters do I want to allow?

我想允许哪些字符?

This is because there could be lots of charactersthat make in into a string somehow that blow stuff up that you wouldn't expect.

这是因为可能有很多字符以某种方式组合成一个字符串,这些字符会破坏你意想不到的东西。

For example this one only allows for letters and numbers removing groups of invalid characters replacing them with a hypen:

例如,这个只允许字母和数字删除无效字符组,用连字符替换它们:

"This¢£?±?÷could&*()\/<>be!@#$%^bad".replace(/([^a-z0-9]+)/gi, '-');
//Result: "This-could-be-bad"

回答by ChaosPandion

You need to wrap them all in a character class. The current version means replace this sequence of characters with an empty string. When wrapped in square brackets it means replace anyof these characters with an empty string.

您需要将它们全部包装在一个字符类中。当前版本意味着用空字符串替换此字符序列。当用方括号括起来时,这意味着用空字符串替换这些字符中的任何一个。

var cleanString = dirtyString.replace(/[\|&;$%@"<>\(\)\+,]/g, "");

回答by Darin Dimitrov

Put them in brackets []:

把它们放在括号里[]

var cleanString = dirtyString.replace(/[\|&;$%@"<>\(\)\+,]/g, "");