views:

54

answers:

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
A: 

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

This works quite well.

chris12892
Thanks for the reply I am using same expression but its not allowing this form of urlwww.google.com
pkumar
Interesting. I am using that very pattern in a few of my applications, and it has always worked really well. Are you sure there isn't an error in your code?
chris12892
Shouldn't it be `((ftp|http|https):\/\/)?...` ?
Felix Kling
+1  A: 
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);
sushil bharwani
it works thanks I am not using it properly
pkumar
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