Created
June 27, 2014 13:00
-
-
Save samsworldofno/25c9d46c9b2b421a11a0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol | |
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name | |
'((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address | |
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path | |
'([\?\&][\\\/;&a-z\\d%_.~+=-]*)?'+ // query string | |
'(\\#[-a-z\\d_]*)?$','i'); // fragment locator | |
function testString(str){ | |
if(!pattern.test(str)) { | |
return false; | |
} else { | |
return true; | |
} | |
} | |
strings = [ | |
'https://google.com', | |
'http://dummyimage.com/600x400/ffffff/000.png', | |
'http://dummyimage.com/600x400/ffffff/000.png&text=1', | |
'http://www.worldofspectrum.org/showscreen.cgi?screen=1', | |
'http://www.worldofspectrum.org/showscreen.cgi?screen=screens.gif', | |
'http://www.worldofspectrum.org/showscreen.cgi?screen=screens/in-game/c/ChaseH.Q..gif' | |
] | |
for(var i=0; i < strings.length; i++) { | |
string = strings[i] | |
result = testString(string) | |
console.log(result + ": " + string) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment