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')