RegExpValidator 无法正确验证 URL 模式 [英] RegExpValidator does not validate a URL pattern correctly

查看:23
本文介绍了RegExpValidator 无法正确验证 URL 模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

URL http://www.ftd.de/rss2 在我检查时无效它在这个(冒险)<mx:RegExValidator> :

The URL http://www.ftd.de/rss2 is not valid when I check it against the below RegEx in this (unadventurous) <mx:RegExValidator> :

<mx:RegExpValidator id="validatorURL" required="true" enabled="true"
    source="{inputURL}" property="text" triggerEvent="focusOut"
    expression="{resourceManager.getString('resources','general.urlRegExp')}"
    flags="i" />

这个带有以下 RegEx 的 RegExValidator 可以正确验证 URL只要它们不以数字结尾.任何其他有效 URL 似乎都有效.

This RegExValidator with the below RegEx validates URLs correctly as long as they don't end with a number. Any other valid URL seems to be valid.

这是为什么?Flex 错误?

Why is that? Flex bug?

我使用 这个 RegEx by Diego Perini,在这个比较中获得了最好的结果.

I use this RegEx by Diego Perini, which gets the best results in this comparison.

^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$

RegEx 本身与上述 URL 配合得很好,它在我迄今为止测试过的任何非 flex 验证器中都能正确验证:

The RegEx itself works great with the above URL, it validates correctly in any non-flex validator i've tested so far:

我已经用两个基于 Flex 的验证器检查了 RegEx,它们都无法验证任何 URL:

I've checked the RegEx with two Flex-based validators, which both failed to validate any URL at all:

我们使用 Flex SDK 4.6.0.23201.

We use Flex SDK 4.6.0.23201.

推荐答案

nhahtdh 的回答让我再次检查这个问题.它没有解决问题,但它为我指明了正确的方向.我将使用他的回答中的 RegExp,因为从 \uhhhh\x{hhhh} 的建议转换也是必要的.

nhahtdh's answer made me check this issue again. It didn't solve the problem, but it pointed me in the right direction. I'll use the RegExp from his answer, as the suggestion conversion from \uhhhh to \x{hhhh} is necessary as well.

实际问题是,当将 RegExp 作为字符串传递给 RegExpValidator 时,它必须双重转义.因此,如果模式是从资源文件加载的,它应该如下所示:

The actual problem is that when passing a RegExp as String to a RegExpValidator it has to be double escaped. So the Pattern should look like this if it is loaded from a resources file:

general.urlRegExp=^(?:(?:https?|ftp):\\/\\/)(?:\\S+(?::\\S*)?@)?(?:(?!10(?:\\.\\d{1,3}){3})(?!127(?:\\.\\d{1,3}){3})(?!169\\.254(?:\\.\\d{1,3}){2})(?!192\\.168(?:\\.\\d{1,3}){2})(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\x{00a1}-\\x{ffff}0-9]+-?)*[a-z\\x{00a1}-\\x{ffff}0-9]+)(?:\\.(?:[a-z\\x{00a1}-\\x{ffff}0-9]+-?)*[a-z\\x{00a1}-\\x{ffff}0-9]+)*(?:\\.(?:[a-z\\x{00a1}-\\x{ffff}]{2,})))(?::\\d{2,5})?(?:\\/[^\\s]*)?$

现在可以直接在RegExpValidator中使用:

Now this can be used directly in the RegExpValidator:

<mx:RegExpValidator expression="{resourceManager.getString('resources','general.urlRegExp')}" flags="gmi" />

这篇关于RegExpValidator 无法正确验证 URL 模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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