-2

How can I change this:

This is a string [[remove]] this

To:

This is a string this

I managed to figure out how to remove a string between one set of brackets but I couldn't get it right with two:

var myString = "This is my string [remove] this"
myString.replace(/ *\[[^\]]*]/, '');
Asp1re
  • 51
  • 5
  • Just double the literal brackets in your regex. Although you only have one opening literal bracket. – VLAZ Dec 16 '19 at 09:46
  • You didn't do anything in that regex to make it work with two!? Did you try to put two of `\[` and `]` in it instead of one?? – trincot Dec 16 '19 at 09:47
  • Add another set of escaped brackets: `\[\[.*\]\]` – adiga Dec 16 '19 at 09:47
  • yes I tried and removes the string but removes everything after also – Asp1re Dec 16 '19 at 09:48
  • @Wiktor I think OP has trouble with replacing two brackets `[[` and `]]` instead of replacing multiple sets of matches. Although the dupe is still relevant for the latter case. – VLAZ Dec 16 '19 at 09:48
  • Yeah, I am looking for a better dupe. It sounded as if OP could not remove more than 1 occurrence – Wiktor Stribiżew Dec 16 '19 at 09:49
  • @WiktorStribiżew I absolutely see where you're coming from with the mention of "couldn't get it right with two". But unfortunately, I'm not sure there is a really good dupe that covers this. It's "how do I match stuff between " (like [this one](https://stackoverflow.com/questions/6208367/regex-to-match-stuff-between-parentheses)?) about the pattern and [What does this regex mean?](https://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) about how to match brackets. – VLAZ Dec 16 '19 at 09:52
  • 1
    @VLAZ Well, this seems to be a pure regex issue, so I added some JS and non-JS related dupe links that dwell on matching double `[[` and `]]`. – Wiktor Stribiżew Dec 16 '19 at 09:55
  • @WiktorStribiżew also found [this question about square brackets specifically](https://stackoverflow.com/questions/2403122/regular-expression-to-extract-text-between-square-brackets) it's almost a complete dupe. EDIT: actually, you've found questions about double square brackets... – VLAZ Dec 16 '19 at 09:55
  • I can remove single [] but the problem appears with two [[]]. I tried all the duplicate posts without any good results. – Asp1re Dec 16 '19 at 09:58

1 Answers1

-1

There is expression without regarding nested [[]] myString.replace(/\[\[[^\]]*\]\]/g,''); It is tricky thing to track nested including of pair of symbols

Oleg Bondarenko
  • 1,430
  • 1
  • 11
  • 15