How to resolve an unexpected quantifier in Javascript regex pattern?

爷,独闯天下 提交于 2019-12-11 13:02:21

问题


Scenario: I've added a client side url validator using the following regex pattern. The pattern is supposed to check whether the URL input matches.

^(?#Protocol)(?:(?:ht|f)tp(?:s?)\:\/\/|~\/|\/)?(?#Username:Password)(?:\w+:\w+@)?(?#Subdomains)(?:(?:[-\w]+\.)+(?#TopLevel Domains)(?:com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|travel|[a-z]{2}))(?#Port)(?::[\d]{1,5})?(?#Directories)(?:(?:(?:\/(?:[-\w~!$+|.,=]|%[a-f\d]{2})+)+|\/)+|\?|#)?(?#Query)(?:(?:\?(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=?(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)(?:&(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=?(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)*)*(?#Anchor)(?:#(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)?$

Issue:

When I debug the ValidateOtherInstituteWebsite JavaScript method via IE, a JavaScript runtime error - unexpected quantifier error is thrown.

Checks:

I implemented the same regex in my C# server side validation method with no error, and the regex matches appropriately. From researching the error it seems the compiler is interpreting some of the regex as code, but I don't see where.

Question:

How can this regex pattern be edited to work with JavaScript?

Code:

function ValidateOtherInstituteWebsite(sender, args) {

    var valid = false;

    alert("debug alert");
    var otherInstituteWebsiteText = $("#vs_institutewebsite").text();
    var otherInstituteWebsiteUrl = otherInstituteWebsiteText;

    var urlValidationRegex = new RegExp("^(?#Protocol)(?:(?:ht|f)tp(?:s?)\:\/\/|~\/|\/)?(?#Username:Password)(?:\w+:\w+@)?(?#Subdomains)(?:(?:[-\w]+\.)+(?#TopLevel Domains)(?:com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|travel|[a-z]{2}))(?#Port)(?::[\d]{1,5})?(?#Directories)(?:(?:(?:\/(?:[-\w~!$+|.,=]|%[a-f\d]{2})+)+|\/)+|\?|#)?(?#Query)(?:(?:\?(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=?(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)(?:&(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=?(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)*)*(?#Anchor)(?:#(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)?$");

    if (urlValidationRegex.test(otherInstituteWebsiteUrl))
    {
        valid = true;
    }

    args.IsValid = valid;

}

Exception screen shot:


回答1:


JS regex does not support comments like (?#...) that can be used in regex flavors that support freespace (/x, verbose) mode, you need to remove all of them.

Use

var urlValidationRegex = /^(?:(?:ht|f)tp(?:s?):\/\/|~\/|\/)?(?:\w+:\w+@)?(?:(?:[-\w]+\.)+(?:com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|travel|[a-z]{2}))(?::[\d]{1,5})?(?:(?:(?:\/(?:[-\w~!$+|.,=]|%[a-f\d]{2})+)+|\/)+|\?|#)?(?:(?:\?(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=?(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)(?:&(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=?(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)*)*(?:#(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)?$/i;

See demo

I also suggest adding a /i case-insensitive modifier.

The only advantage of using a RegExp constructor is that you can easily add comments to blocks of this expression. Then use:

var urlValidationRegex = RegExp("^" + 
      "(?:(?:ht|f)tps?://|~?/)?" + // Protocol
      "(?:\\w+:\\w+@)?" +          //Username:Password
      "(?:(?:[-\\w]+\\.)+" +       //Subdomains
      "(?:com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|travel|[a-z]{2}))" +                                  //TopLevel Domains
      "(?::\\d{1,5})?" +           //Port
      "(?:(?:(?:/(?:[-\\w~!$+|.,=]|%[a-f\\d]{2})+)+|/)+|\\?|#)?" + //Directories
      "(?:(?:\\?(?:[-\\w~!$+|.,*:]|%[a-f\\d{2}])+=?(?:[-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)(?:&(?:[-\\w~!$+|.,*:]|%[a-f\\d{2}])+=?(?:[-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)*)*" +                                   //Query
      "(?:#(?:[-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)?$" //Anchor
);
document.body.innerHTML = urlValidationRegex.test("000");
document.body.innerHTML +="<br/>"+ urlValidationRegex.test("http://stackoverflow.com/questions/35089817/how-to-resolve-an-unexpected-quantifier-in-javascript-regex-pattern/35089871?noredirect=1#comment57910001_35089871");


来源:https://stackoverflow.com/questions/35089817/how-to-resolve-an-unexpected-quantifier-in-javascript-regex-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!