Replace all specified strings in the string: Swtichparse (' ABC=DEF=HJK ', ' | | ') = ' abc| | def| | HJK ', feel a few regular global 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
Replace the first target value encountered
1 function Swtichparse (str,replacestrting,targetstring) {2 return str.replace (replacestrting,targetstring) 3 }4 console.log (swtichparse (' abc=def=hjk ', ' = ', ' | | ')) // abc| | DEF=HJK
URL parsing
1 varStr= ' Http://www.nowcoder.com?key=1&key=2&key=3&test=4#hehe ';2 functionSwtichparse (str,name) {3 varresult={};4Str.replace (/\?? (\w+) = (\w+) &?/g,function(s,k,v) {5if(Result[k]!==undefined) {6RESULT[K] =[].concat (result[k],v);7}Else{8RESULT[K] =v;9 }Ten }) One if(name) { A returnResult[name] -}Else{ - returnresult the } - } -Console.log (Swtichparse (str))//{Key:array (3), Test: "4"} -Console.log (Swtichparse (str, ' key '))//["1", "2", "3"] +Console.log (Swtichparse (str, ' Test '))//4
Array string manipulation