匹配URL的正则表达式(推荐)
更新时间:2016年11月23日 10:44:18 作者:Kris゜
正则表达式(regular expression)描述了一种字符串匹配的模式。本文重点给大家介绍匹配url的正则表达式,感兴趣的朋友一起学习吧
正则表达式(regular expression)描述了一种字符串匹配的模式,可以用来检查一个串是否含有某种子串、将匹配的子串做替换或者从某个串中取出符合某个条件的子串等。
则表达式:
|
1
|
var match = /^((ht|f)tps?):\/\/[\w\-]+(\.[\w\-]+)+([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?$/; |
匹配:
(1)、直接匹配域名地址:
|
1
2
|
var matchString = 'https://i.cnblogs.com/';console.log(match.test(matchString)); // ==> true |
(2)、匹配链接含(*.htm,*.html,*.php,*.aspx...)后缀的地址:
|
1
2
|
var matchString = 'https://i.cnblogs.com/EditPosts.aspx';console.log(match.test(matchString)); // ==> true |
(3)、匹配含参数的地址:
|
1
2
|
var matchString = 'https://i.cnblogs.com/EditPosts.aspx?opt=1';console.log(match.test(matchString)); // ==> true |
使用说明:
(1)、地址必须以http/https/ftp/ftps开头;
(2)、地址不能包含双字节符号或非链接特殊字符。
摘抄自网络,便于检索查找。

浙公网安备 33010602011771号