标签:www. targe get 数组 http 全局 UNC swt split
替换掉字符串里所有指定的字符串: swtichParse(‘abc=def=hjk‘,‘||‘)=>‘abc||def||hjk‘ ,感觉有几点正则的全局g
1 function swtichParse(str,replaceStrting,targetString){ 2 return str.split(replaceStrting).join(targetString) 3 } 4 console.log(swtichParse(‘abc=def=hjk‘,‘=‘,‘||‘))//abc||def||hjk
替换第一个遇到的目标值
1 function swtichParse(str,replaceStrting,targetString){ 2 return str.replace(replaceStrting,targetString) 3 } 4 console.log(swtichParse(‘abc=def=hjk‘,‘=‘,‘||‘))//abc||def=hjk
url解析
1 var str=‘http://www.nowcoder.com?key=1&key=2&key=3&test=4#hehe‘; 2 function swtichParse(str,name){ 3 var result={}; 4 str.replace(/\??(\w+)=(\w+)&?/g,function(s,k,v){ 5 if(result[k] !== undefined){ 6 result[k] = [].concat(result[k],v); 7 }else{ 8 result[k] = v; 9 } 10 }) 11 if(name){ 12 return result[name] 13 }else{ 14 return result 15 } 16 } 17 console.log(swtichParse(str))//{key: Array(3), test: "4"} 18 console.log(swtichParse(str,‘key‘))//["1", "2", "3"] 19 console.log(swtichParse(str,‘test‘))//4
标签:www. targe get 数组 http 全局 UNC swt split
原文地址:https://www.cnblogs.com/studyshufei/p/9064483.html