JavaScript basic replace two chars in same string

前端 未结 3 954
花落未央
花落未央 2021-01-15 07:06

I have:

var foo = \'(bar)\'
foo.replace(\'(\', \'\').replace(\')\', \'\')

So, I get bar without parentheses, is there a better

3条回答
  •  无人及你
    2021-01-15 07:54

    You could use:

    foo = foo.replace(/[()]/g, '');
    

    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.

提交回复
热议问题