Javascript 如何使用javascript转义正则表达式特殊字符?

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

How to escape regular expression special characters using javascript?

javascriptregex

提问by Muneeswaran Balasubramanian

I need to escape the regular expression special characters using java script.How can i achieve this?Any help should be appreciated.

我需要使用 java 脚本转义正则表达式特殊字符。我怎样才能做到这一点?应该感谢任何帮助。



Thanks for your quick reply.But i need to escape all the special characters of regular expression.I have try by this code,But i can't achieve the result.

感谢您的快速回复。但我需要转义正则表达式的所有特殊字符。我已经尝试过这段代码,但我无法达到结果。

RegExp.escape=function(str)
            {
                if (!arguments.callee.sRE) {
                    var specials = [
                        '/', '.', '*', '+', '?', '|',
                        '(', ')', '[', ']', '{', '}', '\'
                    ];
                    arguments.callee.sRE = new RegExp(
                    '(\' + specials.join('|\') + ')', 'gim'
                );
                }
                return str.replace(arguments.callee.sRE, '\');

            }

function regExpFind() {
            <%--var regex = new RegExp("\[munees\]","gim");--%>
                    var regex= new RegExp(RegExp.escape("[Munees]waran"));
                    <%--var regex=RegExp.escape`enter code here`("[Munees]waran");--%>
                    alert("Reg : "+regex);
                }

What i am wrong with this code?Please guide me.

这段代码有什么问题?请指导我。

回答by Mathias Bynens

Use the \character to escape a character that has special meaning inside a regular expression.

使用\字符来转义在正则表达式中具有特殊含义的字符。

To automate it, you could use this:

要自动化它,你可以使用这个:

function escapeRegExp(text) {
  return text.replace(/[-[\]{}()*+?.,\^$|#\s]/g, '\$&');
}

Update:There is now a proposal to standardize this method, possibly in ES2016: https://github.com/benjamingr/RegExp.escape

更新:现在有一个标准化这个方法的提议,可能在 ES2016 中:https: //github.com/benjamingr/RegExp.escape

Update: The abovementioned proposal was rejected, so keep implementing this yourself if you need it.

更新:上述提议已被拒绝,因此如果您需要,请继续自己实施。

回答by Ben Rowe

Use the backslash to escape a character. For example:

使用反斜杠转义字符。例如:

/\d/

This will match \d instead of a numeric character

这将匹配 \d 而不是数字字符

回答by Claudio Redi

With \you escape special characters

\你一起转义特殊字符

Escapes special characters to literal and literal characters to special.

E.g: /\(s\)/matches '(s)' while /(\s)/matches any whitespace and captures the match.

将特殊字符转义为文字,将文字字符转义为特殊字符。

例如:/\(s\)/匹配 '(s)' 而/(\s)/匹配任何空格并捕获匹配。

Source: http://www.javascriptkit.com/javatutors/redev2.shtml

来源:http: //www.javascriptkit.com/javatutors/redev2.shtml