1

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.

3
  • 1
    One of the things you'll need to address is that when you form a regex from strings like that, the things you need to quote are different than when you form the regex with regex literal syntax. Specifically, you don't need "\" before your "/" characters, but you do need to double the "\" characters before things like "d" or "?". Commented Jul 12, 2011 at 16:01
  • 1
    Also the line number of the error is probably not interesting, as the actual error won't be detected until the "RegExp()" function is called and the expression is parsed. The problem is probably not, in fact, with that last part of the regular expression. Commented Jul 12, 2011 at 16:02
  • @Pointy, very good point about the error location not giving the correct spot. Commented Jul 12, 2011 at 17:53

2 Answers 2

3

I use this and it seems to have worked fine:

var re = /^(http[s]?:\/\/){0,1}(www\.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}/;
var isUrl = re.test(message);
Sign up to request clarification or add additional context in comments.

Comments

0

You need to escape the first \ in here:

'(\?[;&a-z\d%_.~+=-]*)?'

becomes

'(\\?[;&a-z\d%_.~+=-]*)?'

3 Comments

This got rid of the invalid quantifier. Now it won't accept the sample string above :/
I'm not sure what else is wrong with your regex, but at minimum, the regex currently requires either an IPv4 address, or a domain suffix (.com, .net, .us), etc. There's something else wrong with it, though I can't put my finger on it.
All of the backslash quotes in the regex except for the ones before "/" characters need to be doubled.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.