这里有你不知道的正则表达式

// 去重
'aaaannnewwrffqdffv'.replace(/(.)+/g, '$1');


// 首字母大写
'aaaa nnne wwr ff qdf fv'.replace(/\b\w/g, function(item){
return item.toUpperCase();
});


// 千分符
'12345678912'.replace(/(\d)(?=(\d{3})+$)/g, '$1,');
'12345678912'.replace(/(\B)(?=(\d{3})+$)/g, '$1,');


// 替换最后连续出现的4
'1234567844912444'.replace(/(4)(?=(4)*$)/g, '5');


// 替换除两边外连续出现的4
'1234567844912444'.replace(/[^4](4+)[^4]/g, function($0, $1){
return $0.replace(/4/g, '5');
});


// 获取url参数2
function parseQueryString(url){
var reg = /\??(\w+)=(\w+)&?/g;

var obj = {}; 

    url.replace(reg, function($0, $1, $2){ 
      obj[$1] = $2; 
    }); 


return obj; 
}


console.log(parseQueryString('https://www.baidu.com/s?wd=parseQueryString&rsv_spt=1&rsv_iqid=0xafe4f98f000020ca&issp=1&f=8&rsv_bp=0&rsv_idx=2&ie=utf-8&rqlang=&tn=94855285_hao_pg&ch=&rsv_enter=1&inputT=1107&rsv_spt=111&rsv_spt=222'))

你可能感兴趣的:(JS,数据/算法)