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

前端 未结 5 1669
[愿得一人]
[愿得一人] 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:29

    What exactly are you trying to match?

    If you don't escape the brackets, they are considered character classes. This:

    /[1\\]/
    

    Matches either a 1 or a backslash. You may want to escape them with one backslash only:

    /\[1\]/
    

    But this won't match either, as you don't have a [1] in your string.

提交回复
热议问题