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
  • 963,270
  • 265
  • 3,804
  • 3,187
Woold
  • 683
  • 9
  • 21

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
  • 62,450
  • 9
  • 45
  • 52