Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 正则表达式|在我的网页的文本字段中格式化数字_Javascript_Jquery_Regex - Fatal编程技术网

Javascript 正则表达式|在我的网页的文本字段中格式化数字

Javascript 正则表达式|在我的网页的文本字段中格式化数字,javascript,jquery,regex,Javascript,Jquery,Regex,我使用正则表达式格式化文本字段中的数字,我实现了在每3位数字后插入逗号的功能,如下所示 123->123 1234->1234 123456->123456 我使用以下函数来实现这一点 function Comma(Num) { //function to add commas to textboxes Num += ''; Num = Num.replace(',', ''); Num = Num.replace(',', ''); Num = Num.re

我使用正则表达式格式化文本字段中的数字,我实现了在每3位数字后插入逗号的功能,如下所示

  • 123->123
  • 1234->1234
  • 123456->123456
我使用以下函数来实现这一点

function Comma(Num) { //function to add commas to textboxes
        Num += '';
        Num = Num.replace(',', ''); Num = Num.replace(',', ''); Num = Num.replace(',', '');
        Num = Num.replace(',', ''); Num = Num.replace(',', ''); Num = Num.replace(',', '');
        x = Num.split('.');
        console.log(x);
        x1 = x[0];
        x2 = x.length > 1 ? '.' + x[1] : '';
        console.log(x1);
        console.log(x2);
        var rgx = /(\d+)(\d{2})/;
        while (rgx.test(x1))
            x1 = x1.replace(rgx, '$1' + ',' + '$2');
        console.log("x1: "+x1);
        return x1 + x2;
    }

<input maxlength="" class="form-control preco number" name="preco[]" onkeyup = "javascript:this.value=Comma(this.value);" required>
function逗号(Num){//向文本框添加逗号的函数
Num+='';
Num=Num.replace(',','');Num=Num.replace(',','');Num=Num.replace(',','');
Num=Num.replace(',','');Num=Num.replace(',','');Num=Num.replace(',','');
x=分割数('.');
控制台日志(x);
x1=x[0];
x2=x.长度>1?'.+x[1]:'';
控制台日志(x1);
控制台日志(x2);
var rgx=/(\d+)(\d{2})/;
while(rgx.测试(x1))
x1=x1.替换(rgx,'$1'+','+'$2');
控制台日志(“x1:+x1”);
返回x1+x2;
}
我想要实现的是以下格式

  • 12->12
  • 123->1,23
  • 1234->12,34
  • 123456->1.234,56
你能告诉我如何使用正则表达式实现这种格式吗?

试试这个

^-?\d{1,3}(,\d{3})*(\.\d\d)?$|^\.\d\d$
结果

1
12
.99
12.34 
-18.34
12,345.67
999,999,999,999,999.99

你能解释一下你想要的格式吗?放置
的规则是什么?而
12345678
123456789
会是什么呢?@SpencerWieczorek a
放在两位数之后,然后每三位数后面放一个
。这将是123.456,78和1.234.567,89!