(*SKIP)(*F) Syntax in Perl and PCRE (PHP, Delphi, R...)
With all the disclaimers about using regex to parse html, we can do this with a surprisingly simple regex:
<[^>]*>(*SKIP)(*F)|(hello)
Sample PHP Code:
$replaced = preg_replace('~<[^>]*>(*SKIP)(*F)|(hello)~i',
'<span style="background:yellow">$1</span>',
$yourstring);
In the regex demo, see the substitutions at the bottom.
Explanation
This problem is a classic case of the technique explained in this question to "regex-match a pattern, excluding..."
The left side of the alternation |
matches complete <tags>
then deliberately fails, after which the engine skips to the next position in the string. The right side captures hello
(case-insensitive to Group 1, and we know they are the right ones because they were not matched by the expression on the left.
Reference