1.千分位分割,手机号码分割
//千分位分割组替换 var n = ‘1234321789.33‘, reg = /(\d{1,3})(?=(\d{3})+(?:\.|$))/g; n = n.replace(reg,‘$1,‘); console.log(‘n: ‘ + n); //千分位分割单词边界 var m = ‘1234321789.33‘, reg = /\B(?=(\d{3})+(?!\d))/g; m = m.replace(reg,‘,‘); console.log(‘m: ‘ + m) //手机号分割单词边界 var p = ‘18810808376‘, reg = /\B(?=(\d{4})+(?!\d))/g; p = p.replace(reg,‘ ‘); console.log(‘p: ‘ + p);
2.获取url参数
function queryAll(){ var src = location.search || location.hash, reg = /[?&]([^&]+)=([^&]+)/g, res = null, obj = {}; while(res = reg.exec(src)){ obj[res[1]] = decodeURIComponent(res[2].replace(/\+/g,‘ ‘)); } return obj; } function queryOne(key){ var src = location.search || location.hash, reg = new RegExp(‘[?&]‘+key+‘=([^&]+)‘), match = reg.exec(src); return match == null ? null : decodeURIComponent(match[1].replace(/\+/g,‘ ‘)) ; }
3.replace的正则形式
//写format方法 function format(s){ var args = arguments; return s.replace(/\{(\d+)\}/,function($0,$1){ return args[($1 | 0) + 1] || ‘‘; }) } format("{0} love {1}.",‘I‘,‘You‘) //I love you