The main point is to match the pattern you would normally put into a negative lookbehind into an optional capturing group, and then check if the group matched. If it did, you do not need the match, else, use it.
If you need to match and collect substrings, use
var re = /(\\?)0x[A-F0-9]{2}/gi;
var str = '\\0x50 0x60 asdasda0x60';
var res = [];
while ((m = re.exec(str)) !== null) {
if (!m[1]) {
res.push(m[0]);
}
}
document.body.innerHTML = "TEST: " + str + "<br/>";
document.body.innerHTML += "RES: " + JSON.stringify(res,0,4) + "<br/>";
If you need to replace only those strings that have no \
before the 0x..
, use a callback within the replace
method to check if Group 1 matched. If it did, replace with the whole match, and if not, just replace with the pattern you need.
var re = /(\\?)0x[A-F0-9]{2}/gi;
var str = '\\0x50 0x60 asdasda0x60';
var res = str.replace(re, function(m, group1){
return group1 ? m : "NEW_VAL";
});
document.body.innerHTML = "TEST: " + str + "<br/>";
document.body.innerHTML += "RES: " + res + "<br/>";