如何在RegExp javascript中输入[](方括号)?

时间:2022-05-15 21:45:26

I'm trying this:

我在这个:

str = "bla [bla]";
str = str.replace(/\\[\\]/g,"");
console.log(str);

And the replace doesn't work, what am I doing wrong?

替换不起作用,我做错了什么?

UPDATE: I'm trying to remove any square brackets in the string, what's weird is that if I do

更新:我正在尝试删除字符串中的任何方括号,如果我这么做,那么奇怪的是

replace(/\[/g, '')
replace(/\]/g, '')

it works, but
replace(/\[\]/g, '');
doesn't.

它可以工作,但是可以替换(/\[\]/g);不喜欢。

4 个解决方案

#1


39  

It should be:

应该是:

str = str.replace(/\[.*?\]/g,"");

You don't need double backslashes (\) because it's not a string but a regex statement, if you build the regex from a string you do need the double backslashes ;).

您不需要双反斜杠(\),因为它不是一个字符串,而是一个regex语句,如果您从一个字符串构建regex,那么您确实需要双反斜杠;)

It was also literally interpreting the 1 (which wasn't matching). Using .* says any value between the square brackets.

它也从字面上解释了1(不匹配)。使用。*表示方括号之间的任何值。

The new RegExp string build version would be:

新的RegExp字符串构建版本将是:

str=str.replace(new RegExp("\\[.*?\\]","g"),"");

UPDATE: To remove square brackets only:

更新:只删除方括号:

str = str.replace(/\[(.*?)\]/g,"$1");

Your above code isn't working, because it's trying to match "[]" (sequentially without anything allowed between). We can get around this by non-greedy group-matching ((.*?)) what's between the square brackets, and using a backreference ($1) for the replacement.

上面的代码不能工作,因为它试图匹配“[]”(顺序上不允许中间有任何东西)。我们可以通过非贪婪的group-匹配((.*?))来解决这个问题,在方括号之间,并使用一个反向引用($1)来替换。

UPDATE 2: To remove multiple square brackets

更新2:删除多个方括号

str = str.replace(/\[+(.*?)\]+/g,"$1");
// bla [bla] [[blaa]] -> bla bla blaa
// bla [bla] [[[blaa] -> bla bla blaa

Note this doesn't match open/close quantities, simply removes all sequential opens and closes. Also if the sequential brackets have separators (spaces etc) it won't match.

注意,这与打开/关闭数量不匹配,只需要删除所有连续的打开和关闭。此外,如果顺序括号中有分隔符(空格等),则不匹配。

#2


1  

You have to escape the bracket, like \[ and \]. Check out http://regexpal.com/. It's pretty useful :)

你必须避开括号,比如\[和\]。看看http://regexpal.com/。它很有用:)

To replace all brackets in a string, this should do the job:

要替换字符串中的所有括号,应该这样做:

str.replace(/\[|\]/g,'');

I hope this helps.
Hristo

我希望这可以帮助。斯托伊

#3


0  

Two backslashes produces a single backslash, so you're searching for "a backslash, followed by a character class consisting of a 1 or a right bracket, and then you're missing an closing bracket.

两个反斜杠产生一个反斜杠,所以您要搜索的是“一个反斜杠,后面跟着一个由1或右括号组成的字符类,然后您将丢失一个右括号。”

Try

试一试

str.replace(/\[1\]/g, '');

#4


0  

What exactly are you trying to match?

你到底想匹配什么?

If you don't escape the brackets, they are considered character classes. This:

如果不转义括号,则将它们视为字符类。这样的:

/[1\\]/

Matches either a 1 or a backslash. You may want to escape them with one backslash only:

匹配1或反斜线。你可能只想用一个反斜杠来转义:

/\[1\]/

But this won't match either, as you don't have a [1] in your string.

但是这个也不匹配,因为你的字符串中没有[1]。

#1


39  

It should be:

应该是:

str = str.replace(/\[.*?\]/g,"");

You don't need double backslashes (\) because it's not a string but a regex statement, if you build the regex from a string you do need the double backslashes ;).

您不需要双反斜杠(\),因为它不是一个字符串,而是一个regex语句,如果您从一个字符串构建regex,那么您确实需要双反斜杠;)

It was also literally interpreting the 1 (which wasn't matching). Using .* says any value between the square brackets.

它也从字面上解释了1(不匹配)。使用。*表示方括号之间的任何值。

The new RegExp string build version would be:

新的RegExp字符串构建版本将是:

str=str.replace(new RegExp("\\[.*?\\]","g"),"");

UPDATE: To remove square brackets only:

更新:只删除方括号:

str = str.replace(/\[(.*?)\]/g,"$1");

Your above code isn't working, because it's trying to match "[]" (sequentially without anything allowed between). We can get around this by non-greedy group-matching ((.*?)) what's between the square brackets, and using a backreference ($1) for the replacement.

上面的代码不能工作,因为它试图匹配“[]”(顺序上不允许中间有任何东西)。我们可以通过非贪婪的group-匹配((.*?))来解决这个问题,在方括号之间,并使用一个反向引用($1)来替换。

UPDATE 2: To remove multiple square brackets

更新2:删除多个方括号

str = str.replace(/\[+(.*?)\]+/g,"$1");
// bla [bla] [[blaa]] -> bla bla blaa
// bla [bla] [[[blaa] -> bla bla blaa

Note this doesn't match open/close quantities, simply removes all sequential opens and closes. Also if the sequential brackets have separators (spaces etc) it won't match.

注意,这与打开/关闭数量不匹配,只需要删除所有连续的打开和关闭。此外,如果顺序括号中有分隔符(空格等),则不匹配。

#2


1  

You have to escape the bracket, like \[ and \]. Check out http://regexpal.com/. It's pretty useful :)

你必须避开括号,比如\[和\]。看看http://regexpal.com/。它很有用:)

To replace all brackets in a string, this should do the job:

要替换字符串中的所有括号,应该这样做:

str.replace(/\[|\]/g,'');

I hope this helps.
Hristo

我希望这可以帮助。斯托伊

#3


0  

Two backslashes produces a single backslash, so you're searching for "a backslash, followed by a character class consisting of a 1 or a right bracket, and then you're missing an closing bracket.

两个反斜杠产生一个反斜杠,所以您要搜索的是“一个反斜杠,后面跟着一个由1或右括号组成的字符类,然后您将丢失一个右括号。”

Try

试一试

str.replace(/\[1\]/g, '');

#4


0  

What exactly are you trying to match?

你到底想匹配什么?

If you don't escape the brackets, they are considered character classes. This:

如果不转义括号,则将它们视为字符类。这样的:

/[1\\]/

Matches either a 1 or a backslash. You may want to escape them with one backslash only:

匹配1或反斜线。你可能只想用一个反斜杠来转义:

/\[1\]/

But this won't match either, as you don't have a [1] in your string.

但是这个也不匹配,因为你的字符串中没有[1]。