How can I put [] (square brackets) in RegExp javascript?

前端 未结 5 1678
[愿得一人]
[愿得一人] 2020-11-27 19:13

I\'m trying this:

str = \"bla [bla]\";
str = str.replace(/\\\\[\\\\]/g,\"\");
console.log(str);

And the replace doesn\'t work, what am I do

5条回答
  •  被撕碎了的回忆
    2020-11-27 19:35

    You have to escape the bracket, like \[ and \]. Check out http://regexpal.com/. It's pretty useful :)

    To replace all brackets in a string, this should do the job:

    str.replace(/\[|\]/g,'');
    

    I hope this helps.
    Hristo

提交回复
热议问题