下面是html页面底部的html页面和脚本。
正则表达式似乎不起作用。
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="">
<p>Input something in the input box:</p>
<p>Name : <input type="text" ng-model="name" placeholder="Enter name here"> </p>
<h1>Hello {{name}}</h1>
</div>
</body>
<script>
//below is the format obtained from the db
var format="##,##,###";
var reverseFormat=Array.from(format).reverse().join('');
var reverseFormatLength=reverseFormat.length;
//below are various separator
var firstseparator=null;
var restSeparator=null;
var firstSeparatorIndicator=0;
var indicesForVariousSeparators=[];
var number='123456524578';
x=number.toString();
var stringToGoIntoTheRegex=2;
var regex = new RegExp("\B(?=(\d{"+ stringToGoIntoTheRegex+"})+(?!\d))" , "g");
var lastThree = x.substring(x.length-4);
var otherNumbers = x.substring(0,x.length-4);
if(otherNumbers != '')
lastThree = ',' + lastThree;
var res = otherNumbers.replace(regex, ",") + lastThree;
alert(res);
</script>
</html>
在上面的代码中,我正在使用
var stringToGoIntoTheRegex=2;
var regex = new RegExp("\B(?=(\d{"+ stringToGoIntoTheRegex+"})+(?!\d))" , "g");
但这似乎不起作用。
输出应该是 :1,23,45,65,24,578
每一次帮助都是宝贵的。