-1

I am looking for it about 2 hours, but can not find what I need.

what I need is very simple:

  1. allow: google.com, http://google.com, https://google.com
  2. disallow spaces "goo gle.com"
  3. with a valid domain: I mean it should have a dot "." + any domain (.com, .net etc.)
  4. and allow anything after that: "googl.com/dsfsdf/sdfs/blablahblah/" without spaces

thanks

Edit: Thanks all, I had to write it myself.

    if (!/^((ftp|http|https):\/\/)?([a-z0-9_\.-]+)\.{1}([a-z0-9_\/\?\=\-\%-]+)$/.test(uri) 
|| /([\._\/\?\=\-\%-])\1/.test(uri)) {                                                     
    }

ps: I am noob in regexs.

user01
  • 23
  • 1
  • 8
  • Can you show your last attempted regex! – anubhava Dec 23 '13 at 06:39
  • ^((ftp|http|https):\/\/)?([a-zA-Z0-9]+(\.[a-zA-Z0-9]+)+.*)$ This allows spaces in domain name "google.c om" – user01 Dec 23 '13 at 06:47
  • This is an excellent example of a question that should have been searched. Next time you have a programming problem, ask yourself "I wonder if anyone else has had to solve this problem before," and if the answer is "Yes", then do a search for it. – Andy Lester Dec 23 '13 at 06:57
  • @AndyLester I dont understand you. – user01 Dec 23 '13 at 07:07
  • The problem that you were facing, "How do I validate a URL", is a very common one. If you think about it, you'll probably realize that you are not the first person to be faced with this problem to solve. So, if you have a very common problem to be solved, the very first thing you should do is search for the problem, such as "how can I validate a URL?", and then use the answer that you find, instead of asking a question that has already been answered a thousand times. – Andy Lester Dec 23 '13 at 15:39

2 Answers2

1
  • www.google.com
  • http://www.google.com
  • mailto:somebody@google.com
  • somebody@google.com
  • www.url-with-querystring.com/?url=has-querystring

The REGEX below matches all the above cases

((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)

REGEX Explanation can be found here

Working Example

Amarnath Balasubramanian
  • 9,300
  • 8
  • 34
  • 62
0

Something that's working for me on a production product (haven't received any complaints yet):

((www\.|(http|https|ftp|news|file)+\:\/\/)?[&#95;.a-z0-9-]+\.[a-z0-9\/&#95;:@=.+?,##%&~-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])
developer82
  • 13,237
  • 21
  • 88
  • 153