url regex without http://www

14,652

It would probably be a good idea to keep the www's intact in order to preserve the sub-domain. A regex pattern like this:

^([a-zA-Z0-9]+(\.[a-zA-Z0-9]+)+.*)$

would match a URL that is not prefixed by a protocol (http://,https://,ftp://,etc).

Share:
14,652

Related videos on Youtube

input
Author by

input

Seeker of knowledge

Updated on June 04, 2022

Comments

  • input
    input almost 2 years

    i need a url regex which validates the url string without http://www. or https://www.

    any ideas?

    • Hinek
      Hinek almost 14 years
      give an example of the string you have and the string you want to extract ... or do you want to validate if the string is an url but does not start with www.?
    • BC.
      BC. almost 14 years
      It seems clear to me. OP wants a url regex pattern that doesn't match "www.". @fusion Why not take an existing one and remove that match from the beginning of the pattern?
    • Lèse majesté
      Lèse majesté almost 14 years
      He could have also meant a regex that strips http://www. from a URL. Since he doesn't specify a use case and apparently spent less than 10 seconds writing the question, it's hard to tell.
    • input
      input almost 14 years
      apologies for not being clear. i want the regex to validate the url string but without http://www. it should only validate google.com not http://www.google.com
    • Lunivore
      Lunivore almost 14 years
      Hi Fusion, do your URLs have ports, parameters, etc.?
    • Kevin Sedgley
      Kevin Sedgley almost 14 years
      Do you want to be able to extract URLs from a string, like gmail chat does, without having to specify 'http://' or 'www.'? So for instance boobi.es would be turned to [boobi.es](boobi.es)? I forsee a lonnnnng regex.
    • sigint
      sigint almost 14 years
      What if a website requires the www subdomain to be valid? Ex. www.example.com/ serves a page, but example.com/ 404's.
    • input
      input almost 14 years
      @lunivore, no. @kevin, yes something like that.
    • input
      input almost 14 years
      @sigint, i'm not removing http://www.. it'd be programmatically appended to the url which the user enters in the textbox. i don't want the user to enter http://www.
    • Quentin
      Quentin almost 14 years
      So run your regular expression against "www." + what_the_user_enters. Hope you don't mind excluding https URIs.
  • input
    input almost 14 years
    thank you! as i said i'm not removing the www. it would be appended to the url which the user enters in the textbox.
  • Joel Etherton
    Joel Etherton almost 14 years
    That regex would allow silliness like subdomain.realdomain.Hambone^&&#$@$%^&* which wouldn't qualify.
  • sigint
    sigint almost 14 years
    @fusion What if example.com doesn't have a www subdomain? You shouldn't blindly prepend the www's. @Joel Etherton You are correct, this regex pattern would be VERY liberal when dealing with (what should be) GET parameters.
  • FOOM
    FOOM over 5 years
    also doesn't allow hyphens