This Javascript that I found here adds a method parseURL() to the string object so that if there are any links inside it will surround them with tags so that they become clickable.
String.prototype.parseURL = function() {
return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&~\?\/.=]+/g, function(url) {
return url.link(url);
});
};
First of all, how does this even work? I know a little about regex expressions and can make simple ones, but I don’t even see the word “href” in here!
How can this be modified so that the link opens in another window? i.e. How can I add the target="_blank" property in here?
According to:
http://www.w3schools.com/jsref/jsref_link.asp
The String.link() method returns a text “blah” as a HTML link
So my guess is you have 2 solutions:
1/ override the link() definition (not recommended), declaring something like:
2/ define another function and use it instead of link() in the definition of parseURL():
Then your code should become:
To explain a bit, replace() returns the input string where parts matching the regular expression (which in this case expresses the shape of any URL) are replaced by the second argument (the unnamed function that returns url.link()). What the JS reference doesn’t say is that if you give a function – instead of a string – as the second argument of replace(), then replace() will provide the currently matching part as an argument to that unnamed argument-function. Sweet mechanics, assuming this works on any browser 😉