admin管理员组

文章数量:1434445

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: .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.

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./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.

Share Improve this question edited May 23, 2017 at 12:27 CommunityBot 11 silver badge asked Jul 12, 2011 at 15:43 John StoneJohn Stone 1371 gold badge2 silver badges10 bronze badges 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 "?". – Pointy 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. – Pointy Commented Jul 12, 2011 at 16:02
  • @Pointy, very good point about the error location not giving the correct spot. – John Stone Commented Jul 12, 2011 at 17:53
Add a ment  | 

2 Answers 2

Reset to default 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);

You need to escape the first \ in here:

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

bees

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

本文标签: javascriptUsing RegEX to match URL patterninvalid quantifierStack Overflow