Using the JS replace function with regex, and will have dozens of replace statements.
var NewHTML = OriginalHTML
.replace(/\bJavaScript\b/gi, "<a href=\"http://js.com/\">$&</a>")
.replace(/\bMySQL\b/gi, "<a href=\"http://www.mysql.com/\">$&</a>")
;
To make it more readable and more manageable (i.e. easier to change the regex pattern or flags by changing it in one place instead of on every line), trying to pull the replace regex condition and replace flags out into a separate variable:
var pattern = /\b(?!\-)(?!\/)\b(?!\-)/gi;
var NewHTML = OriginalHTML
.replace("JavaScript", "<a href=\"http://js.com/\">$&</a>", pattern)
.replace("MySQL", "<a href=\"http://www.mysql.com/\">$&</a>", pattern)
Problem is, the inline call is being completely ignored… both the regex portion and the flags portion.
Can anyone spot what’s wrong with the JS replace call or declaration of the regex/flags variable? 🙂
Thanks!
I would use a lookup object to map the originals to URLs:
Here is a demonstration: http://jsfiddle.net/qP9Er/
EDIT:
As per your request, here is a version that replaces the first n occurrences. You can find a demonstration here: