Javascript正则匹配文件名和文件后缀

//去除字符串前后空格
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;
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值