0

P.S: Please don't mark this as Duplicate, I've seen many other questions.
I have a set of URLs to be validated like:

https://pqr-aaa.abc.xyz.com:8383
http://172.24.100.200:6767
and many more. I'm validating them in javascript. I've seen many questions and found the regex I'm looking for. This seems to suit my need:

^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?|^((http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$


My question is that, is there a universal standard for URL validation regex that is used by everyone, or do we just use the regex suited for our needs?

  • If there exists a post asking about an universal Regexp for URL validation, and an answer providing such a Regexp, why shouldn't this question be marked as a dup? – Teemu Jan 23 '20 at 05:24
  • I've seen that question, I'm asking whether there is a standard that is followed everywhere strictly :( – Sanjay Bharathi Jan 23 '20 at 05:26
  • To me it looks like you're asking a RegExp, there is a [standard](https://www.ietf.org/rfc/rfc1738.txt) though. – Teemu Jan 23 '20 at 05:31
  • @Teemu So, if I tweak my regex to that standard, then I'd be following the norms, right? – Sanjay Bharathi Jan 23 '20 at 05:34
  • 1
    Well, kinda, see also [this answer](https://stackoverflow.com/a/1305082/1169519). – Teemu Jan 23 '20 at 05:34
  • 1
    It appears bobince's answer is a bit outdated, though still correct what comes to regexps. Take a look at [URL](https://developer.mozilla.org/en-US/docs/Web/API/URL/URL) at MDN. – Teemu Jan 23 '20 at 05:42

1 Answers1

0

I believe below regex will work for all the URL validation:

^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$