indexOf: 检测出当前字符在字符串中第一次出现位置的索引
split : 按照某一个字符把字符串拆分成数组中的每一项
方法1 字符串分割法
let url='https://www.baidu.com/s?ie=UTF-8&wd=baidu'
function queryURLParameter(url){
let obj={}
if(url.indexOf('?')<0) return obj;
let array=url.split('?')
url=array[1]
array=url.split('&')
for (let i = 0; i < array.length; i++) {
let cur=array[i],
curAry=cur.split('=');
obj[curAry[0]]=curAry[1]
return obj;
}
queryURLParameter(url)
方法2 正则判断
let url='https://www.baidu.com/s?ie=UTF-8&wd=baidu'
function queryURLParameter(url){
let reg=/([^?=&]+)=([^?=&]+)/g,obj={};
url.replace(reg,function(){
obj[arguments[1]]=arguments[2];
});
queryURLParameter(url)
字符串转数组格式
let arr="小名,小妹,小俪"
let arr1=arr.split(',')
console.log(arr1) // "arr1":["小名","小妹" ,"小俪"],
网友评论