WraithLux
WraithLux

Reputation: 699

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 regexp 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.

Upvotes: 0

Views: 780

Answers (4)

KooiInc
KooiInc

Reputation: 122908

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(','));

Upvotes: 1

Emmerman
Emmerman

Reputation: 2343

For text replace

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

Upvotes: 0

Alan
Alan

Reputation: 652

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

Upvotes: 0

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174299

Use this:

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

Upvotes: 2

Related Questions