I need to remove all occurances of a string but only when it is within 2 tags.
For example in the content below I need to remove both occurances of foo within the [bar][/bar] tags.
foo
foo
[bar]
foo bar foo
[/bar]
foo
I am using PHP and I would like to find a solution which doesn’t involve 2 steps (ie search for the content between [bar][/bar] first and then do the replace). If this is not possible then no problem, any help is much appreciated!
If your tags are all well formed and not nested you may be successful with this
See it here on Regexr
It searches for
foosurrounded by word boundaries\b(foo in foobar will not match), that is followed by a closing tag[/bar]. to ensure that there is no new opening tag I will only match characters other than[with[^\[]*.(?=[^\[]*\[\/bar\])is a lookahead assertion that does not match it only looks ahead if the pattern inside is following.