JavaScript basic replace two chars in same string

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

    If it is always the first and last characters you are trying to get rid of, you could use the slice method: http://www.w3schools.com/jsref/jsref_slice_string.asp

    ​var foo = '(bar)';
    alert(foo.slice(​​​​​​​​1, 4));
    //Or
    alert(foo.slice(1, foo.length - 1));​
    

提交回复
热议问题