javascript - Error occurred in Chrome but not Firefox -
<!doctype html> <html> <head> <meta charset="utf-8"> <title>a demo page</title> </head> <body> <script> var formatnum=function(num) { if(!/^(\+|-)?\d+(\.\d+)?$/.test(num)){ return num; } var re = new regexp().compile("(\\d)(\\d{3})(,|\\.|$)"); num += ""; while(re.test(num)) num = num.replace(re, "$1,$2$3") return num; } alert(formatnum(1000000)); </script> </body> </html>
i use these codes fomat number 1,000,000 works in firefox , not work in chrome
here error shows in chrome uncaught typeerror: cannot call method 'test' of undefined how can resolve error
the regexp().compile() method deprecated.
why don't use regex -:
var regexp = new regexp("(\\d)(\\d{3})(,|\\.|$)");
Comments
Post a Comment