Reputation: 173
I have a long text which contains some tags to highlight the text between. I need to match the text between these tags. Here is the list of tags: CA | OU | TC.
So I did this:
\[[CA|OU|TC]+\][\s\S]*?\[-[CA|OU|TC]+\]
It matches correctly these strings
[CA] bla bla [-CA]
[OU] bla[-OU]
[TC] blabla [-TC]
The problem is that I need to only match if the tags are the same. It should not match the following.
[CA] bla bla [-OU]
Any ideas? Thank you.
Upvotes: 1
Views: 38
Reputation: 626738
You may use
\[(CA|OU|TC)][\s\S]*?\[-\1]
See the regex demo.
The (CA|OU|TC)
forms a capturing group and \1
matches the same value.
var s = '[CA] bla bla [-CA] [OU] bla[-OU] [TC] blabla [-TC]] [CA] bla bla [-OU]';
var rx = /\[(CA|OU|TC)][\s\S]*?\[-\1]/g;
console.log( s.match(rx) );
Upvotes: 1