开发者

javascript regexp find 3rd char at the end of the string

How do I use regexp to turn "1.500.00" into "1.500,00"? The comma is always needed before 2 last digits. So I need re开发者_Python百科gexp to look at the end of the string and replace 3rd char with ",". But I can't figure out what expression to use for this.


Use this:

yourString.replace(/\.(\d\d)$/, ",$1");


How about not using a regexp:

var num = '1.500.00'.split('.'), 
    num1 = num.slice(0,num.length-1), 
    num2 = num[num.length-1];

alert(num1.join('.')+','+num2); //=> 1.500,00

Or without intermediate variables (num1, num2):

alert(num.slice(0,num.length-1).join('.')+','+num[num.length-1]); /=> 1.500,00

Or

alert([ num.slice(0,num.length-1).join('.'), num[num.length-1] ].join(','));


str = "1.500.00";

var patt1=/.\d{2}$/;
var patt2=/\d{2}$/;
document.write(str.replace(str.match(patt1),','+str.match(patt2)));

result:

1.500,00


For text replace

"I have 1.400.00$ and 57.60 pounds".replace(/\.(\d{2}\D?)/g, ",$1")

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜