Description
This regex will match John providing it is either at the start or end of the string and/or has white space on either side.
Regex to match John: (?:\s| |^)(John)(?=\s|\r|\n|$)
This regex incorporates that last regex and also matches all html tags and plain text urls. The order here is important because John will only match providing it's outside an html tag or not embeded into a URL.
Regex: https?:\/\/[^\s]*|<\/?\w+\b(?=\s|>)(?:='[^']*'|="[^"]*"|=[^'"][^\s>]*|[^>])*>|\ John|(John)
If you take this last regex and pass it through your function, then only Johns outside the tags & urls will be replaced with a string.
Javascript Example
Working example: http://repl.it/J4T
Code
var content = "<span name=\"John\" funnytag:John>John John John DoeJohn JohnDoe Mr.JohnDoe http://cool.guy.john/LikesKittens</span>";
var rePattern = /https?:\/\/[^\s]*|<\/?\w+\b(?=\s|>)(?:='[^']*'|="[^"]*"|=[^'"][^\s>]*|[^>])*>|\ John|(John)/gi;
content.replace(rePattern, function(match, capture) {
return capture ? "<img src=\"images/user.png\">" : match;
});
Output
<span name="John" funnytag:John><img src="images/user.png"> <img src="images/user.png"> John Doe<img src="images/user.png"> <img src="images/user.png">Doe Mr.<img src="images/user.png">Doe http://cool.guy.john/LikesKittens</span>
Johnwith an image, except those that are inside attributes. Is that right? Or is it required that, in addition, a precedes the wordJohn(like#is the hashtag for twitter)?