1.js獲取地址參數(shù)
Location 對象是 Window 對象的一個部分,可通過 window.location 屬性來訪問。
| 屬性 | 描述 |
|---|---|
| hash | 設(shè)置或返回從井號 (#) 開始的 URL(錨)。 |
| host | 設(shè)置或返回主機(jī)名和當(dāng)前 URL 的端口號。 |
| hostname | 設(shè)置或返回當(dāng)前 URL 的主機(jī)名。 |
| href | 設(shè)置或返回完整的 URL。 |
| pathname | 設(shè)置或返回當(dāng)前 URL 的路徑部分。 |
| port | 設(shè)置或返回當(dāng)前 URL 的端口號。 |
| protocol | 設(shè)置或返回當(dāng)前 URL 的協(xié)議。 |
| search | 設(shè)置或返回從問號 (?) 開始的 URL(查詢部分)。 |
注意:search和hash的區(qū)別,、
如果URL中“?”之前有一個“#”比如:“http://localhost:8888/index.html#/xxx?type=35&id=5”,
那么使用window.location.search得到的就是空(“”)。
因?yàn)椤?type=35&id=5”是屬于“#/version?type=35&id=5”這個串字符的,
也就是說查詢字符串search只能在取到“?”后面和“#”之前這個區(qū)間的內(nèi)容,如果“#”之前沒有“?”,search取值為空。
需要正則驗(yàn)證
正則驗(yàn)證大全地址(https://www.cnblogs.com/hai-ping/articles/2997538.html)
function getUrlSearch(name) {
if(!name){
return null
}
// 查詢參數(shù):先通過search取值,如果取不到就通過hash來取
var after = window.location.search;
//var after="http://localhost:8888/index.html#/ooxx?type=35&id=5"
after =after.substring(after.lastIndexOf('/') + 1)|| window.location.hash.split('?')[1];
if (!after){
return null;
}
//看這個參數(shù)有沒有在地址中有沒有
if (after.indexOf(name) === -1) {
return null;
}
var reg = new RegExp("(^||&)"+ name + "=([^&]*)(&|$)","i");
// 當(dāng)?shù)刂窓趨?shù)存在中文時,需要解碼,不然會亂碼
var r = decodeURI(after).match(reg);
if(!r){
return null;
}
return r[2];
}
console.log(getUrlSearch("type"))//35
2.js獲取字符串中的中文
if (aiMena !== null && aiMena !== "") {
let reg = /[\u4e00-\u9fa5]/g;
return aiMena.match(reg).join("");
} else {
return aiMena;
}