I am trying to use RegEX to match a URL pattern. I found an answer here: Check if a Javascript string is a url
Which pointed me to here: http://forums.devshed.com/javascript-development-115/regexp-to-match-url-pattern-493764.html
Which gave me the following code(cut and pasted from devshed to here and to my script):
function ValidURL(str) {
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 locater
if(!pattern.test(str)) {
alert("Please enter a valid URL.");
return false;
} else {
return true;
}
}
When I attempt to use it in Firefox 4.0, Firebug 1.7.3 gives me an invalid quantifier error at:
'(\#[-a-z\d_]*)?$','i'); // fragment locater
Does anyone have an idea on what the issue might be?
From other searches on invalid quantifier, I believe that * is an issue but not sure what it might be. The string in question that I am using the function on when it gives me the error is:
htp://localhost:1987/
ADDED COMMENT The fix suggested by agent-j at least removed the invalid quantifier issue. thumbs up
However, it doesn't like the sample url above when done correctly:
http://localhost:1987/
The issue is with the port. When I remove the port #, it likes localhost.