1

This is url validation regex

/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/

This works Okay. However if I type url with port number such as http://localhost:3000, it gives an error.

How can I change this expression to accept a port number as well?

GoonGamja
  • 1,936
  • 5
  • 20
  • 46

1 Answers1

3

Please can try this one

https?:\/\/(?:w{1,3}\.)?[^\s.]+(?:\.[a-z]+)*(?::\d+)?(?![^<]*(?:<\/\w+>|\/?>))

https?: This part matches the protocol of the URL. The s is optional, so it matches both http and https.

:\/\/: This matches the colon and two forward slashes that are part of the protocol.

(?:w{1,3}\.)?: This matches the optional www subdomain, if it exists. The (?: ) creates a non-capturing group, and the {1,3} quantifier matches between 1 and 3 instances of the letter "w".

[^\s.]+: This matches the domain name, which consists of one or more characters that are not whitespace or a dot.

(?:\.[a-z]+)*: This matches any optional subdomains that come before the domain, separated by dots. The (?: ) creates another non-capturing group, and the * quantifier matches zero or more instances of this group. The [a-z]+ matches one or more lowercase letters.

(?::\d+)?: This matches an optional port number that might come after the domain, preceded by a colon. The (?: ) creates another non-capturing group, and the ? quantifier makes it optional. The : matches the colon, and \d+ matches one or more digits.

(?![^<]*(?:<\/\w+>|\/?>)): This is a negative lookahead that ensures that the URL is not part of an HTML tag. It checks if there are no characters before the URL that match the pattern (?:<\/\w+>|\/?>), which matches any closing HTML tag or an empty tag.

  • 1
    Please add some explanation to your answer such that others can learn from it. What did you change, and why? – Nico Haase Apr 17 '23 at 06:57