2

I am new to JavaScript

i need a regular expression which

allows both of this forms

for example :

  1. http://www.google.com
  2. www.google.com
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
pkumar
  • 21
  • 3
  • 1
    How about `google.com` and `to`? – BalusC May 24 '10 at 18:58
  • 1
    possible duplicate of [What is the best regular expression to check if a string is a valid URL](http://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url) – Georg Fritzsche May 24 '10 at 19:01

4 Answers4

1
var url_pattern = new RegExp("((ftp|http|https)(:\/\/))?([a-zA-Z0-9]+[.]{1}){2}[a-zA-z0-9]+(\/{1}[a-zA-Z0-9]+)*\/?", "i");

return url_pattern.test(url);
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
sushil bharwani
  • 29,685
  • 30
  • 94
  • 128
  • Hey chris i have one more question I have used the code for same url http://stackoverflow.com/questions/2899454/regular-expression-for-browser-url its not working – pkumar May 24 '10 at 19:34
0

(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?

This works quite well.

chris12892
  • 1,634
  • 2
  • 18
  • 36
0

^((?:(?:https?|ftp):)?\/\/?)?(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$

Test it! https://regex101.com/r/qQ8uV6/1

Extracted from In search of the perfect URL validation regex https://mathiasbynens.be/demo/url-regex (modified it a bit).

credits to @diegoperini.

If you test it in JavaScript, you'll get a nice

ParseError: Error parsing regular expression: Invalid regular expression

....

Range out of order in character class

You need to replace \x{xxxx} for \uxxxx. So in JavaScript it'll be:

^(?:(?:https?|ftp):\/\/)?(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\ufff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$

Just like stated in JavaScript Unicode Regex - Range out of order in character class

Community
  • 1
  • 1
I.G. Pascual
  • 5,818
  • 5
  • 42
  • 58
0

I've just written up a blog post on recognising URLs in most used formats such as:

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

The regular expression used is /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/ however I would recommend you got to http://blog.mattheworiordan.com/post/13174566389/url-regular-expression-for-links-with-or-without-the to see a complete working example along with an explanation of the regular expression in case you need to extend or tweak it.

Matthew O'Riordan
  • 7,981
  • 4
  • 45
  • 59