In a single regex, we match the part that would normally be skipped with a lookbehind asserrtionhref="<<merge_field>>
and also the part that you need <<merge_field>>
.
This is the ONLY way to work around the lookbehind deficit in JS.
This works by completely consuming the bad part, and moves the current position past it
There is NO other way, don't be fooled.
This is done within a string replace callback.
Within the callback you can do the logic to control what get's written back as part of the replacement.
In our callback, if group 1 is matched, we just return group 1 thus not changing it's content.
If group 2 is matched, you can decide whether to just return group 2 changing nothing
(if you're just recording its position or pushing an array element),
or just change it to what you want.
var target = "href=\"<<merge_field>> and <<merge_field>> and href=\"<<merge_field>>";
var targ_new =
target.replace( /(href=["']<<merge_field>>)|(<<merge_field>>)/g,
function( match, grp1, grp2 ) {
if ( grp1 )
return grp1;
return "MRG"; // or return grp2 if just recording location
}
);
console.log( "Before:\r\n" + target);
console.log( "After:\r\n" + targ_new );