Wraps all URLs in anchor tags with a href
and target
inside some given text.
Name | Type | Description |
---|---|---|
text | String | The text that may or may not containing URLs. |
new_window | Boolean | Determines if the URLs should be opened in a new window (optional - defaults to true ). |
Returns a String in which URLs have been replaced with anchors tag linking to the URL.
wrapURLs('Google is pretty awesome! Take a look at http://www.google.com.');
// Returns 'Google is pretty awesome! Take a look at <a href="http://www.google.com" target="_blank">http://www.google.com</a>.'
The URL Regex used here is taken from @diegoperini submission at https://mathiasbynens.be/demo/url-regex.
For the JS version, It can be added as a String prototype like this:

String.prototype.wrapURLs = function(new_window){ // same body of the original function, simply replaced the parameter "text" with the keyword "this" }
then calling it like so:

Just sharing this, since the original function has been very useful for me :)
Thanks