从 Javascript 中的字符串中删除括号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10844194/
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
Remove Parenthesis from String in Javascript
提问by nocturn4l toyou
x = x.replace(/[{()}]/g, '');
y = y.replace(/[{()}]/g, '');
x = x.replace(/[\[\]']+/g, '');
y = y.replace(/[\[\]']+/g, '');
okay I understand that the first block removes the curly brackets, and the second block of code removes the regular brackets. I want to remove parenthesis now.. can someone show me how?
好的,我知道第一个块删除了大括号,第二个代码块删除了常规括号。我现在想删除括号..有人可以告诉我怎么做吗?
I've gotten the above code by just googling.. but I don't understand "how" they come up with this, can someone please explain? thanks
我只是通过谷歌搜索得到了上面的代码..但我不明白他们“如何”想出这个,有人可以解释一下吗?谢谢
回答by Vitim.us
First Regex
第一个正则表达式
x = x.replace(/[{()}]/g, '');
y = y.replace(/[{()}]/g, '');
In your first regex /[{()}]/g
the outer square brackets []
makes a character class, it will match one of the character specified inside of it. In this case the characters {
(
)
}
.
在您的第一个正则表达式中/[{()}]/g
,外方括号[]
构成一个字符类,它将匹配其中指定的一个字符。在这种情况下,字符{
(
)
}
.
Outside of the /regexp/
you have the g
(global) modifiermeaning that your entire regular expression will match as many times as it can , and it will not just make to the first match.
在 之外,/regexp/
您有g
(全局)修饰符,这意味着您的整个正则表达式将匹配尽可能多的次数,并且不会只匹配第一个匹配项。
Second regex
第二个正则表达式
x = x.replace(/[\[\]']+/g, '');
y = y.replace(/[\[\]']+/g, '');
In your second regex /[\[\]']+/g
the outer square brackets []
makes a character class, it will match one of the character specified inside of it. In this case the characters [
]
'
.
在您的第二个正则表达式中/[\[\]']+/g
,外部方括号[]
构成一个字符类,它将匹配其中指定的字符之一。在这种情况下,字符[
]
'
.
Note that the square brackets appear scaped inside the [character class]as \[
\]
.
请注意,方括号在[character class] 中显示为\[
\]
.
After it you have specified a +
quantifier, it makes the preceding rule match one or more
times in a row. Note that this is redundant, even if it works, this is not quite what you want.
在您指定了一个+
量词之后,它会使前面的规则one or more
连续匹配时间。请注意,这是多余的,即使它有效,这也不是您想要的。
Outside of the /regularexpression/
you have the g
(global) modifiermeaning that your entire regular expression will match as many times as it can , and it will not just make to the first match.
在 之外,/regularexpression/
您有g
(全局)修饰符,这意味着您的整个正则表达式将匹配尽可能多的次数,并且不会只匹配第一个匹配项。
Suggested Solution
建议的解决方案
run1.onclick = function() {
//removes "(" and ")"
output1.innerHTML = input1.value.replace(/[()]/g, '');
}
run2.onclick = function() {
//removes (){}[]
output2.innerHTML = input2.value.replace(/[\])}[{(]/g, '');
}
<p>Remove ()</p>
<input id="input1" type="text" value="(123) 1234-1234">
<input id="run1" type="button" value="run">
<span id="output1"></span>
<hr>
<p>Remove ()[]{}</p>
<input id="input2" type="text" value="Hello (this) is [] a {{test}}!">
<input id="run2" type="button" value="run">
<span id="output2"></span>