如何使用javascript / jquery中的逗号分隔符格式化和取消格式化美元金额?

时间:2015-04-24 08:58:43

标签: javascript jquery regex

我尝试使用以下正则表达式中的逗号分隔符格式化美元金额:

$.fn.digits = function(){ 
    return this.each(function(){ 
        $(this).text( $(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") ); 
    })
}

<div>$1000000</div>

$('#divid').digits();

这样可以正常格式化金额,但如何取消格式化金额字段?

还有其他一些简单的方法吗?

1 个答案:

答案 0 :(得分:1)

您可以使用RegEx.replace方法替换所有不必要的字符,如此

$.fn.unformat = function(){ 
    return this.each(function() { 
        $(this).text($(this).text().replace(/[^0-9$]/g, '')); 
    })
}

Example

相关问题