常用的正则表达式集锦
1、根据输入的url,获取页面的后缀名
//检测url合法性
var url = 'http://www.iis7.com/b/plc/?1-31.html';
//提取文件后缀
var reg = / var reg = /^(:?http|https):\/\/((:?[a-z0-9]*?\.){2,3})(:?com|net|cn|tv|org|edu|hk|gov){1}.*?\/.*?\.(html|shtml|htm|php|jsp|aspx)(:?\?*|\?*.*?)$/i;
/;
//匹配 test 提取match
var result = url.match(reg);
console.log(result);
function parseURL(url) {
var a = document.createElement('a');
a.href = url;
return {
source: url,
protocol: a.protocol.replace(':',''),
host: a.hostname,
port: a.port,
query: a.search,
params: (function(){
var ret = {},
seg = a.search.replace(/^\?/,'').split('&'),
len = seg.length, i = 0, s; //len = 2
alert(a.search)
for (;i<len;i++) {
if (!seg[i]) { continue; }
s = seg[i].split('=');
ret[s[0]] = s[1];
}
return ret;
})(),
file: (a.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1],
hash: a.hash.replace('#',''),
path: a.pathname.replace(/^([^\/])/,'/$1'),
relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [,''])[1],
segments: a.pathname.replace(/^\//,'').split('/')
};
}