Assuming a well-formed html document with outer/enclosing tags like <html>
, I would think the easiest way would be to look for the >
and <
signs:
/(\>[^\>\<]*)ZZZ([^\>\<]*\<)/$1AAA$2/
If you're dealing with HTML fragments that may not have enclosing tags, it gets a little more complicated, you'd have to allow for start of string and end of string
Example JS (sorry, missed the tag):
alert('<A HREF="ZZZ">test test ZZZ<SPAN>ZZZ test test</SPAN></A>'.replace(/(\>[^\>\<]*)ZZZ([^\>\<]*\<)/g, "$1AAA$2"));
Explanation: for each match that
- starts with
>
: \>
- follows with any number of characters that are neither
>
nor <
: [^\>\<]*
- then has "ZZZ"
- follows with any number of characters that are neither
>
nor <
: [^\>\<]*
- and ends with
<
: \<
Replace with
- everything before the ZZZ, marked with the first capture group (parentheses):
$1
- AAA
- everything after the ZZZ, marked with the second capture group (parentheses):
$2
Using the "g" (global) option to ensure that all possible matches are replaced.