Find URL’s in string and convert them to links – JavaScript

If you need to find the url's in a string and convert them to HTML link elements using JavaScript, feel free to take this handy regex expression:

function parseLinks(string) {
  const exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig
  return string.replace(exp,`<a href='$1'>$1</a>`)

console.log ( parseLinks('Follow this') );

// Output:
// Follow this <a href='' target='_blank'></a>"


About Ricard Torres

Senior Front-end Software Engineer from Barcelona, Haidong Gumdo Instructor (korean martial art of the sword), street photographer, travel lover, TV addict, Boston Red Sox fan, and privacy advocate.

@ricard_dev @ricard_dev

📝 Blog 🎙 Podcast

Leave a Reply

Add <code> Some Code </code> by using this tags.