开发者

jQuery Regular Express Reverse Order

I'm trying my mimic the comma insertion for input type='number' in FireFox since it doesn't support it yet. Naively it works in Chrome. I got my regex to work...just not in the right order, I need to reverse it.

http://jsfi开发者_JAVA技巧ddle.net/hCTDV/2/

You can see that it will format the number as 123,4 instead of 1,234. I tried 'reverse()' but I can't get that to work, Firefox states it's not a function.

If somebody could adjust my regex so it properly performs the task I want that will be great, otherwise running it in reverse might do.


Try this method:

  $('input').keyup(function(){
    var $this = $(this);
    var num = $this.val().replace(/,/g, '');
    $this.val(num.replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,"));
  });

It seems a bit simpler and appears to work - http://jsfiddle.net/hCTDV/3/


You can use something like:

var str = "foo 123456789.22 bar";    
str = str.replace(/\d(?=(?:\d{3})+(?!\d))/g, '$&,');

http://jsfiddle.net/Ar3Qv/

Note that this will only work as long as the decimal part is no more than 3 digits, else it will add , in the decimal part too.


what you can do you can reverse string then apply regex to it then reverse it again, solution is below:

 $(this).val( $(this).val().replace(".","").split("").reverse().join("").replace(/([0-9]{3}(?=([0-9])))/g, "$1,").split(""),reverse().join("");

I hope this helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜