我一直在尝试将输入格式设置为每三个字符有一个空格,直到句点字符。
例如:
999999999 => 999 999 999
33333.25 => 33 333.25
222.32 => 222.32
4444 => 4444
这是我到目前为止所拥有的:
$(this).on('keyup', function(){
$(this).val( $(this).val().replace(/(\d{3})(?=.)/g, "$1 ") );
});
但这会导致这样的结果:
999999999 => 999 999 999 好的
33333.25 => 333 33.25 不好
222.32 => 222 .32 不好
4444 => 444 4 不行
您可以使用这个基于前瞻的正则表达式:
str = str.replace(/(?!^)(?=(?:\d{3})+(?:\.|$))/gm, ' ');
正则表达式演示 https://regex101.com/r/xF1oZ5/1
正则表达式分解:
(?!^) # Assert we are not at start of line
(?= # start of positive lookahead
(?:\d{3})+ # assert there are 1 or more of 3 digit sets ahead
(?:\.|$) # followed by decimal point or end of string
) # end of lookahead
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)