Convert Text URLs into Links


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. linkify_plain:function(text)
  2. {
  3. if( !text ) return text;
  4.  
  5. text = text.replace(/((https?\:\/\/|ftp\:\/\/)|(www\.))(\S+)(\w{2,4})(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi,function(url){
  6. nice = url;
  7. if( url.match('^https?:\/\/') )
  8. {
  9. nice = nice.replace(/^https?:\/\//i,'')
  10. }
  11. else
  12. url = 'http://'+url;
  13.  
  14.  
  15. return '<a target="_blank" rel="nofollow" href="'+ url +'">'+ nice.replace(/^www./i,'') +'</a>';
  16. });
  17.  
  18. return text;
  19. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.