I have a problem with the javascript replace function and I don’t succeed to resolve it.
This is my code : https://jsfiddle.net/r36k20sa/1/
var tags = ['zazie', 'johnny'];
tags.forEach(function(element) {
content = content.replace(
new RegExp("(?!<a.*?>.*?)(\b" + element + "\b)(?!.*?<\/a>)", "igm"),
'<a href="" class="esk-seo-plu-link" style="background:red;color:white">$1</a>'
);
});
In the tags array, if I reverse the array “johnny” then “zazie” all tags are well selected otherwise, some tags are missing. (The last in this example). What can be the trick?
What can be explained that ? It seems like the javascript replace function runs asynchronous?
Thanks for your help.
2
Answers
Are you seriously using regex to process HTML when you have a DOM parser at your fingertips?
See it in action
Please replace
.
with\.