Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the regular expression of matching URL url by js

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

这篇文章主要为大家展示了"js如何实现匹配网址url的正则表达式",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"js如何实现匹配网址url的正则表达式"这篇文章吧。

DNS规定,域名中的标号都由英文字母和数字组成,每一个标号不超过63个字符,也不区分大小写字母。标号中除连字符(-)外不能使用其他的标点符号。级别最低的域名写在最左边,而级别最高的域名写在最右边。由多个标号组成的完整域名总共不超过255个字符。所以验证则网址url的正则可以如下几种

方法一:

function checkUrl(urlString){if(urlString!=""){var reg=/(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/;if(!reg.test(urlString)){alert("不是正确的网址吧,请注意检查一下"); } }}

方法二:推荐

function IsURL(str_url){ var strRegex = "^((https|http|ftp|rtsp|mms)?://)" + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //ftp的user@ + "(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184 + "|" // 允许IP和DOMAIN(域名) + "([0-9a-z_!~*'()-]+\.)*" // 域名- www. + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // 二级域名 + "[a-z]{2,6})" // first level domain- .com or .museum + "(:[0-9]{1,4})?" // 矶钓- :80 + "((/?)|" // a slash isn't required if there is no file name + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$"; var re=new RegExp(strRegex); //re.test() if (re.test(str_url)){ return (true); }else{ return (false); } }var testUrl;testUrl="http://harveyzeng.iteye.com/blog/1776991";//var testUrl="https://www.jb51.net/article/1.htm";alert(IsURL(testUrl));

刚发现一个不错的多功能测试函数的代码:

/** * 正则表达式判断网址是否有效 */ (function(){ "use strict"; var urlDict=[ //Bad Case 'www.baidu.com', //常规网址,未带协议头的地址 'w.baidu.com', //常规网址,短子域名 'baidu.com', //常规网址,仅有主域名 '测试.com', //非常规合法网址,中文域名不在参考之列 '1.2', //错误域名 ' WWWW ', //无效字符串 '111测试', //无效字符串 //Correct Case 'http://baidu.com', //常规网址,仅有主域名 'http://www.baidu.com', //常规网址,带子域名 'https://www.baidu.com/', //常规网址,使用https协议头,带根目录 'http://www.baidu.com/api', //常规网址,有一级目录下资源 'http://www.subdomain.baidu.com/index/subdir', //常规网址,多级子域名,多级目录 'http://www.www.subdomain.baidu.com/index/subdir/',//常规网址,多级子域名,多级目录,目录地址闭合 'http://io.io' //非常规网址,多级子域名,多级目录,目录地址闭合 ]; // 建议的正则 function isURL(str){ return !!str.match(/(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/g); } // 不知道谁写的简单版的坑爹正则 function badRegFn(str){ return !!str.match(/(http[s]?|ftp):\/\/[^\/\.]+?\..+\w$/g); } //jb51 function IsURL(str_url){ var strRegex = "^((https|http|ftp|rtsp|mms)?://)" + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //ftp的user@ + "(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184 + "|" // 允许IP和DOMAIN(域名) + "([0-9a-z_!~*'()-]+\.)*" // 域名- www. + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // 二级域名 + "[a-z]{2,6})" // first level domain- .com or .museum + "(:[0-9]{1,4})?" // 矶钓- :80 + "((/?)|" // a slash isn't required if there is no file name + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$"; var re=new RegExp(strRegex); //re.test() if (re.test(str_url)){ return (true); }else{ return (false); } } // 测试用例覆盖 (function(){ var ret={}; var collect=function(link){ var obj={},fnList=[isURL,badRegFn,IsURL]; for(var i=0,j=fnList.length;i

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report