javascript - Validating url with http:www as optional using regex -


i trying validate url "http:www" optional, yahoo.com , http://www.yahoo.com needs valid url using following regex not take utl3 valid 1 . how can fix ??

function checkurltest(url){             var urlregex = new regexp("^(https?:\/\/www\.)?(^(https?:\/\/www\.)[0-9a-za-z]+\.+[a-z]{2,5})");              return urlregex.test(url);         }         url3 = "yahoo.com";         url4 = "www.yahoo.com";          alert(checkurltest(url3)); 

working demo http://jsfiddle.net/fy66p/

solution reside here: negative lookahead: http://www.regular-expressions.info/lookaround.html#lookahead www case , should looking for. lemme know how goes!

hope fits needs :)

code

function checkurltest(url){      // try     var urlregex = new regexp("^(?!www | www\.)[a-za-z0-9_-]+\.+[a-za-z0-9.\/%&=\?_:;-]+$")              return urlregex.test(url);         }         url3 = "yahoo.com";         url4 = "www.yahoo.com";          alert('===> ' + checkurltest(url4) + '===> ' + checkurltest(url3)); 

Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -