//去除字符串前后空格
String.prototype.trim = function() {
return this.replace(/(^\s+)|(\s+$)/g, '');
};
//11位手机号格式化
function telFormat(tel, space) {
space = space ? space : ' ';
return String(tel).replace(/(\d{3})(\d{4})(\d{3})/, '$1' + space + '$2' + space + '$3');
}
//获取路径文件信息(文件名、后缀名)
/*var path = "C:\\Users\\sungang\\Desktop\\temp\\email\\index.html";
var href = window.location.href;
var url = 'https://www.example.com/search/LM358.html?type=1&num=1';*/
function getFileInfo(filePath, key) {
filePath = filePath.split('?')[0]; //去参数
var re = /([^\.\/\\]+)\.([a-z]+)$/i,
resultArr = re.exec(filePath),
info = {};
if (resultArr) {
info.name = resultArr[1];
info.type = resultArr[2];
}
return key ? info[key] : info;
}
/*console.log(getFileInfo(path));
console.log(getFileInfo(href));
console.log(getFileInfo(url));
console.log(getFileInfo(url, 'type'));*/
//获取链接字符串参数
function getParams(path) {
var obj = {};
path = path ? path : window.location.href;
//[^\?&=]+ 表示不含?或&或=的连续字符,加上()就是提取对应字符串
path.replace(/([^\?&=]+)=([^\?&=]*)/gi, function(rs, $1, $2, offset, source) {
obj[$1] = $2;
})
return obj;
}
Javascript正则匹配文件名和文件后缀
最新推荐文章于 2024-06-24 12:58:17 发布