URL正则表达式验证 [英] URL regex validation

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

问题描述

我做到了:

 /^(http[s]?://){0,1}(www.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}

并使用验证器进行检查,但在我的页面上它无效:

and checked it with a validator but on my page it is not working:

var re = /^(http[s]?://){0,1}(www.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1};
if (!re.test(url)) { 
    alert("url error");
    return false;
}

我收到此错误

Webpage error details

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)
Timestamp: Tue, 30 Nov 2010 14:23:10 UTC


Message: Expected ')' in regular expression
Line: 781
Char: 23
Code: 0
URI: http://*************************


推荐答案

你必须转义你的特殊字符( / 在这个案例中 www 之后)并且缺少尾随的 / ,如下所示:

You have to escape your special characters (/ and that . after www in this case) and att the missing trailing /, like this:

var re = /^(http[s]?:\/\/){0,1}(www\.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}/;
if (!re.test(url)) { 
    alert("url error");
    return false;
}

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

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