如何解决Javascript正则表达式模式中的意外量词? [英] How to resolve an unexpected quantifier in Javascript regex pattern?

查看:103
本文介绍了如何解决Javascript正则表达式模式中的意外量词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:
我使用以下正则表达式模式添加了一个客户端url验证器。该模式应该检查URL输入是否匹配。

  ^(?#Protocol)(?:( ?: ht | f)tp(?:s?)\: \ / \ / |〜\ / | \ /)?(?#Username:Password)(?: \w +:\w + @)?(?#Subdomains)(?:(?:[- \w] + \。)+(?#TopLevel Domains)(?: com | org | net | gov | mil | biz | info | mobi | name | aero | jobs |博物馆|旅行| [az] {2 }))(?#Port)(?:: [\d] {1,5})?(?#Directories)(?:(?:( ?: \ /(?:[-\w〜 !$ + |。,=] |%[af\d] {2})+)+ | \ /)+ | \?|#)?(?#Query)(?:( ?: \ ?(?:[-\w〜!$ + |。,*:] |%[af\d {2}])+ =?(?:[-\w〜!$ + |。,* :=] |%[af\d] {2})*)(?:&(?:[-\w〜!$ + |。,*:] |%[af\d {2} ])+ =?(?:[-\w〜!$ + |。,*:=] |%[af\d] {2})*)*)*)*(?#Anchor)(?:# (?:[-\w〜!$ + |。,*:=] |%[af\d] {2})*)?$ 

问题:



当我调试 ValidateOtherInstituteWebsite 通过IE的JavaScript方法,会出现 JavaScript运行时错误-意外的量词错误。



检查:



我在C#服务器端验证方法中实现了相同的正则表达式,没有错误,并且正则表达式正确匹配。通过研究错误,似乎编译器正在

解决方案

JS正则表达式不支持(?#...)之类的注释,这些注释可用于支持自由空间( / x ,详细)模式,则需要删除所有这些。



使用

  var urlValidationRegex = / ^(?:(?:( ?: ht | f)tp(?:s?):\ / \ / |〜\ / | \ /)?(?: \w +:\w + @)?(?:(?:[-\w] + \。)+(?: com | org | net | gov | mil | biz | info | mobi | name | aero | jobs |博物馆|旅行| [az] {2}))((::: [\d] {1,5})?(?:(?:( ?: \ /(?:[-\ w〜!$ + |。,=] |%[af\d] {2})+)+ | \ /)+ | \?|#)?(?:( ?: \?(? :[-\w〜!$ + |。,*:] |%[af\d {2}])+ =?(?:[-\w〜!$ + |。,*:=] |%[af\d] {2})*)(?:&(?:[-\w〜!$ + |。,*:] |%[af\d {2}])+ =?(?:[-\w〜!$ + |。,*:=] |%[af\d] {2})*)*)*)*)*(?:#(?:[-\w 〜!$ + |。,*:=] |%[af\d] {2})*)?$ / i; 

请参见演示



我还建议添加 / i 不区分大小写的修饰符。



使用RegExp构造函数的唯一好处是,您可以轻松地向该表达式的块添加注释。然后使用:



  var urlValidationRegex = RegExp( ^ +(?:(?:ht | f)tps?:// | 〜?/)? + //协议(?:\\w +:\\w + @)? + //用户名:密码(?:(?:[-\\w] + \\。)+ + //子域(?:com | org | net | gov | mil | biz | info | mobi | name | aero | jobs | museum | travel | [az] {2}) ) + //顶级域名(?:: \\d {1,5})? + //端口(?:(?:(?:/(?:[-\\w 〜!$ + |。,=] |%[af\\d] {2})+)+ | /)+ | \\?|#)? + //目录(?:( ?:\\?(?:[-\\w〜!$ + |。,*:] |%[af\\d {2}])+ =?(?:[-\ \\w〜!$ + |。,*:=] |%[af\\d] {2})*)(?:&(?:[-\\w〜!$ + |。,*:] |%[af\\d {2}])+ =?(?:[-\\w〜!$ + |。,*:=] |%[af\ \d] {2})*)*)*)* + //查询(?:#(?:[-\\w〜!$ + |。,*:=] |%[af\ \d] {2})*)?$ //锚点;; document.body.innerHTML = urlValidationRegex.test( 000); document.body.inn erHTML + =< br /> + urlValidationRegex.test( http://stackoverflow.com/questions/35089817/how-to-resolve-an-unexpected-quantifier-in-javascript-regex-pattern/35089871 ?noredirect = 1#comment57910001_35089871);  


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:

解决方案

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");

这篇关于如何解决Javascript正则表达式模式中的意外量词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆