0

Is it possible to match multiple occurrances of a regular expression in a string for example, I wanna know if my string contains multiple url and I want to get a usable result like an array:

"hey check out http://www.example.com and www.url.io".match(new RegExp("([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+@)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?([^ ])+"))

would return:

["http://www.example.com","www.url.io"]

console.log("hey check out http://www.example.com and www.url.io".match(new RegExp("([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+@)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?([^ ])+")))

and maybe there is a better way to match urls but i didn't find it

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Woold
  • 783
  • 12
  • 23

1 Answers1

1

You can try the following RegEx:

/(http?[^\s]+)|(www?[^\s]+)/g

Demo:

function urlify(text) {
  var urlRegex = /(http?[^\s]+)|(www?[^\s]+)/g;
  return text.match(urlRegex);
}

console.log(urlify("hey check out http://www.example.com and www.url.io"));
Mamun
  • 66,969
  • 9
  • 47
  • 59