I'm using this regex /\[(.*?)\]/gi
to highlight all texts inside brackets including the brackets.
However, it's giving me a lot of false positives, and I'd like to limit the highlights to brackets that contain specific words and symbols.
Those are: 1. RSVP
& 2. §
So for example, I have the ff:
[RSVP dog] [§111] [cat]
It shouldn't target [cat]
anymore.
How do I go about doing this if it's possible?
Thank you in advance!
CodePudding user response:
You can use
\[[^\]\[]*(?:RSVP|§)[^\]\[]*]
See the regex demo.
Details:
\[
- a[
char[^\]\[]*
- zero or more chars other than[
and]
(depending on regex flavor, you might need to use[^][]*
or[^\][]*
)(?:RSVP|§)
-RSVP
or§
[^\]\[]*
- zero or more chars other than[
and]
(depending on regex flavor, you might need to use[^][]*
or[^\][]*
)]
- a]
char (it must be escaped only in some very few regex flavors, so you might need to use\]
).