js常用方法,JS实用方法,jq获得文件后缀,解析window。location,解析URL参数

jq获取文件后缀的方法

var file = "测试文档1111111111.docx"; // 文件
var fileName = file.replace(/.*(\/|\\)/, ""); // 获得文件名
var fileSuffix = (/[.]/.exec(fileName)) ? /[^.]+$/.exec(fileName.toLowerCase()) : ''; // 获得文件后缀
var file = "测试文档1111111111.docx";
var dot = file.lastIndexOf(".");
var type = file.substr(dot);
var fileSuffix = "";
if(type === ".jpg" || type === ".gif" || type === ".JPG" || type === ".GIF" || type === ".pdf" || type === ".docx" || type === ".doc"){
    fileSuffix = type;
}

jq解析window.location

console.log(window.location.href) // 当前URL;例子:https://baijiahao.baidu.com/s?id=1625846473808878131&wfr=spider&for=pc
console.log(window.location.protocol) // 协议;例子:https:
console.log(window.location.hostname) // 域名;例子:baijiahao.baidu.com
console.log(window.location.origin) // 协议+域名;例子:https://baijiahao.baidu.com
console.log(window.location.search) // 请求参数;例子:?id=1625846473808878131&wfr=spider&for=pc

jq解析URL参数

function getParameter(name) {
    // 正则:[找寻'&' + 'url参数名字' = '值' + '&']('&'可以不存在)
    let reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    let r = window.location.search.substr(1).match(reg);
    if(r != null) {
      // 对参数值进行解码
        return unescape(r[2]); 
    }
    return null;
}

// 调用方法,注意需要传入String类型的数据,输出结果为String类型
getParameter('id');   // '123'
// 使用正则表达式来匹配全路径下的?后的参数,避免#的干扰
const getUrlValue = function (name) {
  let href = location.href
  return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(href) || [, ""])[1].replace(/\+/g, '%20')) || null
}
 
getUrlValue('inApp')

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值