nadir
nadir

Reputation: 1323

How can I write a Regex for French number formatting? (space as thousands separator, comma as decimal separator)

I'm having a JQuery function that displays an amount in a text input field. Very simple... However this is a French app, and I'm trying to build a regex for French number formatting (space as thousands separator, comma as decimal separator).

I've tried several ways... and the last try was .replace(/(\d)(?=(?:\d{3})+(?:$))/g, '$1 '); but it gives no space as thousands separator, and no comma decimal separator. Thanks in advance for any help!

Upvotes: 0

Views: 1434

Answers (1)

mhusaini
mhusaini

Reputation: 1252

You could reverse the string and then try replace(/(\d{3})/g, "$1 ") and then reverse again. Here's a jsFiddle.

NOTE: The reverse method used in the fiddle is from this question.

UPDATE

You might have to first break the string into whole number and fractional parts, perform the above transformation, and put it back together again with a comma (if necessary). It is perhaps uglier than what you are looking for, but I couldn't think of a purely regex solution. I have updated the jsFiddle accordingly.

Upvotes: 1

Related Questions