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.

Community
  • 1
  • 1
John Stone
  • 137
  • 1
  • 2
  • 10
  • 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 "?". – Pointy Jul 12 '11 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. – Pointy Jul 12 '11 at 16:02
  • @Pointy, very good point about the error location not giving the correct spot. – John Stone Jul 12 '11 at 17:53

2 Answers2

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);
slandau
  • 23,528
  • 42
  • 122
  • 184
0

You need to escape the first \ in here:

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

becomes

'(\\?[;&a-z\d%_.~+=-]*)?'
agent-j
  • 27,335
  • 5
  • 52
  • 79
  • This got rid of the invalid quantifier. Now it won't accept the sample string above :/ – John Stone Jul 12 '11 at 17:51
  • 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. – agent-j Jul 12 '11 at 19:08
  • **All** of the backslash quotes in the regex *except* for the ones before "/" characters need to be doubled. – Pointy Jul 12 '11 at 21:17