This question already has an answer here:
这个问题已经有了答案:
- Is there a RegExp.escape function in Javascript? 12 answers
- Javascript中是否有RegExp.escape函数?12个答案
I have code like:
我有这样的代码:
pattern = 'arrayname[1]'; // fetch from dom, make literal here just for example
reg = new RegExp(RegExp.quote(pattern), 'g');
mystring.replace(reg, 'arrayname[2]');
but it fails with an error message saying: "RegExp.quote is not a function".
但是它失败了,错误消息说:“RegExp。引号不是函数。
Am I missing something simple?
我错过了什么简单的东西吗?
8 个解决方案
#1
170
This question got me searching on Google for a RegEx.quote
function in JavaScript, which I was not aware of. It turns out that the function exists in only one place, namely in an answer by Gracenote here on *. The function is defined like this:
这个问题让我在谷歌上搜索RegEx。JavaScript中的quote函数,我不知道。结果表明,函数只存在于一个地方,即在*上的Gracenote的一个答案。函数的定义如下:
RegExp.quote = function(str) {
return (str+'').replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&");
};
If you wish to use this function, you will need to include the above definition somewhere above the point where you use the function in your script.
如果您希望使用这个函数,那么您需要将上面的定义包含在您在脚本中使用该函数的点之上。
#2
24
If you're replacing literally, you don't need a regexp in the first place:
如果按字面意思替换,首先不需要regexp:
str = str.split(search).join(replace)
#3
23
From the mozilla dev docs
来自mozilla开发文档
function escapeRegExp(string){
return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
}
This is out of the ordinary, but in this particular scenario, I would create a function like this
这是不寻常的,但是在这个特殊的场景中,我要创建一个这样的函数
RegExp.escape = function(str) {
return String(str).replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
};
Usage
使用
new RegExp(RegExp.escape('http://www.google.com'));
//=> /http\:\/\/www\.google\.com/
#4
17
var easiest = any_string.replace(/\W/g, "\\$&");
EDIT:
编辑:
Why should I remember which characters have a special meaning or even use a function if escaping any non-word character is enough?
为什么我要记住哪些字符有特殊的含义,如果转义任何非文字字符就足够了,为什么还要使用函数呢?
My solution is a no brainer, maybe that's why it gets down voted. :D
我的解决办法很简单,也许这就是为什么投票失败的原因。:D
#5
15
Here is the exact function Google's closure library uses.
下面是谷歌的闭包库使用的确切函数。
/**
* Escapes characters in the string that are not safe to use in a RegExp.
* @param {*} s The string to escape. If not a string, it will be casted
* to one.
* @return {string} A RegExp safe, escaped copy of {@code s}.
*/
goog.string.regExpEscape = function(s) {
return String(s).replace(/([-()\[\]{}+?*.$\^|,:#<!\\])/g, '\\$1').
replace(/\x08/g, '\\x08');
};
See link
看到链接
#6
5
Mozilla recommends to use this function to espace character from regex :
Mozilla建议将此函数用于regex中的espace字符:
function escapeRegExp(string){
return string.replace(/([.*+?^${}()|\[\]\/\\])/g, "\\$1");
}
You can find this one at the end of this chapter in Mozilla Javascript Guide: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Using_Special_Characters
您可以在本章末尾的Mozilla Javascript指南中找到这个:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Using_Special_Characters
#7
4
Previous answers escape too much characters.
之前的回答会漏掉太多字符。
According to What special characters must be escaped in regular expressions?, only the following characters need to be escaped:
根据正则表达式中哪些特殊字符必须转义?,只有以下字符需要转义:
-
.^$*+?()[{\|
outside character classes. - ^ $ * + ?()[{ \ |字符类。
-
^-]\
inside character classes. - ^ -]\字符类。
Then, this function does the trick:
然后,这个函数的作用是:
function escapeRegExp(str) {
return str.replace(/[.^$*+?()[{\\|\]-]/g, '\\$&');
}
#8
2
Well, first of all you can define the regular expression with its own constant syntax:
首先,你可以用它自己的常量语法定义正则表达式:
var reg = /arrayname\[1\]/;
Inside the regular expression you quote things with backslash. Now, if you're starting from a string, you have to "protect" those backslashes inside the string constant. In that case, the pattern is being parsed twice: once when the string constant is gobbled by the Javascript parser, and then once by the RegExp constructor:
在正则表达式中,引用反斜杠。现在,如果你从一个字符串开始,你必须“保护”字符串常量中的反斜线。在这种情况下,将对模式进行两次解析:一次是Javascript解析器对字符串常量的处理,另一次是RegExp构造函数:
var pattern = "arrayname\\[1\\]";
var reg = new RegExp(pattern);
The backslashes are doubled so that the string "pattern" will look like the regular expression in my first example - one backslash before each bracket character.
反斜杠增加了一倍,使字符串“模式”看起来像我的第一个示例中的正则表达式——每个括号字符前的一个反斜杠。
#1
170
This question got me searching on Google for a RegEx.quote
function in JavaScript, which I was not aware of. It turns out that the function exists in only one place, namely in an answer by Gracenote here on *. The function is defined like this:
这个问题让我在谷歌上搜索RegEx。JavaScript中的quote函数,我不知道。结果表明,函数只存在于一个地方,即在*上的Gracenote的一个答案。函数的定义如下:
RegExp.quote = function(str) {
return (str+'').replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&");
};
If you wish to use this function, you will need to include the above definition somewhere above the point where you use the function in your script.
如果您希望使用这个函数,那么您需要将上面的定义包含在您在脚本中使用该函数的点之上。
#2
24
If you're replacing literally, you don't need a regexp in the first place:
如果按字面意思替换,首先不需要regexp:
str = str.split(search).join(replace)
#3
23
From the mozilla dev docs
来自mozilla开发文档
function escapeRegExp(string){
return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
}
This is out of the ordinary, but in this particular scenario, I would create a function like this
这是不寻常的,但是在这个特殊的场景中,我要创建一个这样的函数
RegExp.escape = function(str) {
return String(str).replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
};
Usage
使用
new RegExp(RegExp.escape('http://www.google.com'));
//=> /http\:\/\/www\.google\.com/
#4
17
var easiest = any_string.replace(/\W/g, "\\$&");
EDIT:
编辑:
Why should I remember which characters have a special meaning or even use a function if escaping any non-word character is enough?
为什么我要记住哪些字符有特殊的含义,如果转义任何非文字字符就足够了,为什么还要使用函数呢?
My solution is a no brainer, maybe that's why it gets down voted. :D
我的解决办法很简单,也许这就是为什么投票失败的原因。:D
#5
15
Here is the exact function Google's closure library uses.
下面是谷歌的闭包库使用的确切函数。
/**
* Escapes characters in the string that are not safe to use in a RegExp.
* @param {*} s The string to escape. If not a string, it will be casted
* to one.
* @return {string} A RegExp safe, escaped copy of {@code s}.
*/
goog.string.regExpEscape = function(s) {
return String(s).replace(/([-()\[\]{}+?*.$\^|,:#<!\\])/g, '\\$1').
replace(/\x08/g, '\\x08');
};
See link
看到链接
#6
5
Mozilla recommends to use this function to espace character from regex :
Mozilla建议将此函数用于regex中的espace字符:
function escapeRegExp(string){
return string.replace(/([.*+?^${}()|\[\]\/\\])/g, "\\$1");
}
You can find this one at the end of this chapter in Mozilla Javascript Guide: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Using_Special_Characters
您可以在本章末尾的Mozilla Javascript指南中找到这个:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Using_Special_Characters
#7
4
Previous answers escape too much characters.
之前的回答会漏掉太多字符。
According to What special characters must be escaped in regular expressions?, only the following characters need to be escaped:
根据正则表达式中哪些特殊字符必须转义?,只有以下字符需要转义:
-
.^$*+?()[{\|
outside character classes. - ^ $ * + ?()[{ \ |字符类。
-
^-]\
inside character classes. - ^ -]\字符类。
Then, this function does the trick:
然后,这个函数的作用是:
function escapeRegExp(str) {
return str.replace(/[.^$*+?()[{\\|\]-]/g, '\\$&');
}
#8
2
Well, first of all you can define the regular expression with its own constant syntax:
首先,你可以用它自己的常量语法定义正则表达式:
var reg = /arrayname\[1\]/;
Inside the regular expression you quote things with backslash. Now, if you're starting from a string, you have to "protect" those backslashes inside the string constant. In that case, the pattern is being parsed twice: once when the string constant is gobbled by the Javascript parser, and then once by the RegExp constructor:
在正则表达式中,引用反斜杠。现在,如果你从一个字符串开始,你必须“保护”字符串常量中的反斜线。在这种情况下,将对模式进行两次解析:一次是Javascript解析器对字符串常量的处理,另一次是RegExp构造函数:
var pattern = "arrayname\\[1\\]";
var reg = new RegExp(pattern);
The backslashes are doubled so that the string "pattern" will look like the regular expression in my first example - one backslash before each bracket character.
反斜杠增加了一倍,使字符串“模式”看起来像我的第一个示例中的正则表达式——每个括号字符前的一个反斜杠。