<script type="text/javascript">
function addCommas(val){
while(/(\d+)(\d{3})/.test(val.toString())){
val = val.toString().replace(/(\d+)(\d{3})/,'$1'+','+'$2')
return val
}
}
var a = 10001111;
console.log('addCommas(10001111): '+addCommas(10001111)); // 10001,111
function addCommas2(val){
return val.toString().replace(/\B(?=(\d{3})+(?!\d))/g,',')
}
console.log('addCommas2(10001111): '+addCommas2(10001111.11111)); //10,001,111.11,111
function addCommas3(val){
var sIntNum = val.toString(),
bIndex = sIntNum.indexOf('.');
return sIntNum.replace(/\d(?=(?:\d{3})+(?:\.|$))/g,function($0,i){
return bIndex < 0 || i < bIndex?($0 + ',' ): $0
})
}
console.log('addCommas3(10001111): '+addCommas3(10001111.111111)); //10,001,111.111111
var test = '中国人中国话';
test.replace(/中国(?=人)/,function($0,$1){
console.log($0) //中国
})
var test2 = '地中国人美国人';
test2.replace(/地(?=中国)/,function($0,$1){
console.log($0) //地
})
var test3 = '中国人美国人';
test3.replace(/(?=中国)人/,function($0,$1){
console.log($0) // null
})
// 结论找(?=中国)的前面
//
var test4 = '中国人美国人';
test4.replace(/(?<=中国)人/,function($0,$1){
console.log($0) // null
})
var t = 'Windows2000';
t.replace(/Windows(?=95|98|NT|2000)/,function(i){
console.log(i)
})
</script>
第三种方法OK
(pattern)
匹配pattern并获取这一匹配。所获取的匹配可以从产生的Matches集合得到,在VBScript中使用SubMatches集合,在JScript中则使用$.0…$9属性。要匹配圆括号字符,
请使用“\(”或“\)”。
(?:pattern)
非获取匹配,匹配pattern但不获取匹配结果,不进行存储供以后使用。这在使用或字符“(|)”来组合一个模式的各个部分是很有用。例如“industr(?:y|ies)”
就是一个比“industry|industries”更简略的表达式。
(?=pattern)
非获取匹配,正向肯定预查,在任何匹配pattern的字符串开始处匹配查找字符串,该匹配不需要获取供以后使用。例如,“Windows(?=95|98|NT|2000)”能匹配“Windows2000”
中的“Windows”,
但不能匹配“Windows3.1”中的“Windows”。
非获取匹配,正向否定预查,在任何不匹配pattern的字符串开始处匹配查找字符串,该匹配不需要获取供以后使用。例如“Windows(?!95|98|NT|2000)”
能匹配“Windows3.1”中的“Windows”,但不能匹配“Windows2000”中的“Windows”。
(?<=pattern)
非获取匹配,反向肯定预查,与正向肯定预查类似,只是方向相反。例如,“(?<=95|98|NT|2000)Windows”能匹配“2000Windows”中的“Windows”,
但不能匹配“3.1Windows”中的“Windows”。