Chen Li Yong
Chen Li Yong

Reputation: 6107

Is there any url regex validator that fits this criteria?

I need a url validator regex with this criteria:

So based on the criteria, these should pass:

These should not pass:

The closest regex I've found is from here:

^((?:.|\n)*?)((http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)([-A-Z0-9.]+)(/[-A-Z0-9+&@#/%=~_|!:,.;]*)?(\?[A-Z0-9+&@#/%=~_|!:‌​,.;]*)?)

But unfortunately, google.com doesn't pass. It needs to have www. as a prefix. Can you improve this regex so www. becomes optional?

Upvotes: 2

Views: 696

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627082

It looks like the following pattern matches your criteria:

^(?:https?:\/\/(?:www\.)?|https:(?:\/\/)?)?\w+(?:[-.]\w+)+(?:\/[^\/\s]+)*$

See the regex demo. Details:

  • ^ - start of the string
  • (?:https?:\/\/(?:www\.)?|https:(?:\/\/)?)? - an optional sequence of:
    • https?:\/\/(?:www\.)? - http or https, :// and then an optional www. substring
    • | - or
    • https:(?:\/\/)? - https: and then an optional // string
  • \w+ - one or more word chars
  • (?:[-.]\w+)+- one or more sequences of a . or - followed with one or more word chars
  • (?:\/[^\/\s]+)* - an optional sequence of a / an then one or more chars other than / and whitespace
  • $ - end of string.

Upvotes: 0

Related Questions