JavaScript基本替换相同字符串中的两个字符 [英] JavaScript basic replace two chars in same string
本文介绍了JavaScript基本替换相同字符串中的两个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有:
var foo = '(bar)'
foo.replace('(', '').replace(')', '')
所以,我得到 bar
没有括号,有更好的方法吗?
So, I get bar
without parentheses, is there a better way to do this?
推荐答案
你可以使用:
foo = foo.replace(/[()]/g, '');
这涉及一个简单的正则表达式,它匹配所有开括号或近括号的实例。
请注意,您必须包含该分配,因为.replace()不会修改字符串;它返回修改后的字符串。
That involves a simple regular expression that matches all instances of either open- or close-parentheses. Note that you have to include that assignment, as ".replace()" does not modify the string; it returns the modified string.
这篇关于JavaScript基本替换相同字符串中的两个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文