'How to highlight brackets [] and the text within IF it contain specific words or symbol using RegEx?
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!
Solution 1:[1]
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|§)-RSVPor§[^\]\[]*- 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\]).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Wiktor Stribiżew |
