Skip to content

Instantly share code, notes, and snippets.

@karmiphuc
Created January 20, 2014 08:05
Show Gist options
  • Save karmiphuc/8516571 to your computer and use it in GitHub Desktop.
Save karmiphuc/8516571 to your computer and use it in GitHub Desktop.
URL Parsing from text
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);
});
};
String.prototype.parsePseudoURL = function() {
return this.replace(/(^| )([^\/])((www|[^\/\. ]+)\.[\S]+(\b|$|))/gim, '$1<a href="http://$2$3" target="_blank">$2$3</a>');
};
s = 'Tinhte.vn Đời có bao lâu mà hững hờ www.google.com.vn xxx google.com.vn xxx Tôi yêu Google http://google.com.vm/search/asdajs.html';
s = s.parseURL().parsePseudoURL();
alert(s);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment