JavaScript basic replace two chars in same string

前端 未结 3 980
花落未央
花落未央 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:50

    You could use a regex capturing everything inside parentheses in a capturing group as per Javascrip Regex e.g.

    var foo = "(bar)";
    var replacedStr = foo.replace(/\((.*)\)/g, "$1");
    

    or replacing just the parentheses with empty string e.g.

    var foo = "(bar)";
    var replacedStr = foo.replace(/[()]/g, ""); 
    

提交回复
热议问题