用于Javascript正则表达式的转义字符串

Escape string for use in Javascript regex

本问题已经有最佳答案,请猛点这里访问。

Possible Duplicate:
Is there a RegExp.escape function in Javascript?

我正在尝试基于用户输入构建一个javascript regex:

1
2
3
4
function FindString(input) {
    var reg = new RegExp('' + input + '');
    // [snip] perform search
}

但是,当用户输入包含?*时,regex将无法正常工作,因为它们被解释为regex specials。实际上,如果用户在字符串中放入不平衡的([,则regex甚至无效。

在regex中正确转义所有特殊字符的javascript函数是什么?


短甜

1
2
3
function escapeRegExp(string) {
  return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}

例子

1
2
3
escapeRegExp("All of these should be escaped: \ ^ $ * + ? . ( ) | { } [ ]");

>>>"All of these should be escaped: \\ \^ \$ \* \+ \? \. \( \) \| \{ \} \[ \]"

安装

在NPM上可用作转义字符串regexp

1
npm install --save escape-string-regexp

注释

参见mdn:javascript指南:正则表达式

其他符号(~ `!@#…)可以毫无后果地逃走,但不必如此。

.

.

.

.

测试用例:典型的URL

1
2
3
escapeRegExp("/path/to/resource.html?search=query");

>>>"\/path\/to\/resource\.html\?search=query"

长的答案

如果你要使用上面的函数,至少在你的代码文档中链接到这个堆栈溢出的帖子,这样它就不会看起来像疯狂的难以测试巫毒。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
var escapeRegExp;

(function () {
  // Referring to the table here:
  // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/regexp
  // these characters should be escaped
  // \ ^ $ * + ? . ( ) | { } [ ]
  // These characters only have special meaning inside of brackets
  // they do not need to be escaped, but they MAY be escaped
  // without any adverse effects (to the best of my knowledge and casual testing)
  // : ! , =
  // my test"~!@#$%^&*(){}[]`/=?+\|-_;:'",<.>".match(/[\#]/g)

  var specials = [
        // order matters for these
         "-"
        ,"["
        ,"]"
        // order doesn't matter for any of these
        ,"/"
        ,"{"
        ,"}"
        ,"("
        ,")"
        ,"*"
        ,"+"
        ,"?"
        ,"."
        ,"\"
        ,"
^"
        ,"
$"
        ,"
|"
      ]

      // I choose to escape every character with '\'
      // even though only some strictly require it when inside of []
    , regex = RegExp('[' + specials.join('\') + ']', 'g')
    ;

  escapeRegExp = function (str) {
    return str.replace(regex,"
\\$&");
  };

  // test escapeRegExp("
/path/to/res?search=this.that")
}());