abstract:正则表达式(regular expression)描述了一种字符串匹配的模式,可以用来检查一个串是否含有某种子串、将匹配的子串做替换或者从某个串中取出符合某个条件的子串等。则表达式:var match = /^((ht|f)tps?):\/\/[\w\-]+(\.[\w\-]+)+([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&
正则表达式(regular expression)描述了一种字符串匹配的模式,可以用来检查一个串是否含有某种子串、将匹配的子串做替换或者从某个串中取出符合某个条件的子串等。
则表达式:
var match = /^((ht|f)tps?):\/\/[\w\-]+(\.[\w\-]+)+([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?$/;
匹配:
(1)、直接匹配域名地址:
var matchString = 'https://i.cnblogs.com/'; console.log(match.test(matchString)); // ==> true
(2)、匹配链接含(*.htm,*.html,*.php,*.aspx...)后缀的地址:
var matchString = 'https://i.cnblogs.com/EditPosts.aspx'; console.log(match.test(matchString)); // ==> true
(3)、匹配含参数的地址:
var matchString = 'https://i.cnblogs.com/EditPosts.aspx?opt=1'; console.log(match.test(matchString)); // ==> true
使用说明:
(1)、地址必须以http/https/ftp/ftps开头;
(2)、地址不能包含双字节符号或非链接特殊字符。
更多关于匹配URL的正则表达式请关注PHP中文网(www.php.cn)其它文章!