0

I want to detect urls in a text and i have achieved that using the following regex

const urlRegex = /(https?:\/\/[^\s]+)/g;

This detects the urls when starting with https or http. But i want to detect a url starting with www as well. How can i modify this regex to detect urls starting with www also?

CraZyDroiD
  • 6,622
  • 30
  • 95
  • 182

1 Answers1

2

One option is to alternate between https?:// and www.. Also note that [^\s] should probably be avoided - better to use \S, which will match all non-whitespace characters, which is easier to read:

/(?:https?:\/\/|www\.)\S+/

https://regex101.com/r/nZhWZY/1

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320