[英]How to insert a space every three characters UP TO a period character?
我一直在嘗試將輸入格式化為每三個字符一個空格,直到句點字符。
例如:
999999999 => 999 999 999
33333.25 => 33 333.25
222.32 => 222.32
4444 => 4 444
這是我到目前為止:
$(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, ' ');
RegEx分手:
(?!^) # 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.