Convert links to hyperlinks in JS

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

Copy this code and paste it in your HTML
  1. function replaceURLWithHTMLLinks(text) {
  2. // this looks for urls in html and makes them links
  3. // I apologize in advance
  4. var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
  5. return text.replace(exp, "<a href='$1'>$1</a>");
  6. }

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.