0

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!

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
nadir
  • 1,223
  • 4
  • 12
  • 21

1 Answers1

1

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.

Community
  • 1
  • 1
mhusaini
  • 1,232
  • 11
  • 18
  • I guess that doesn't account for the decimal separator. – mhusaini Aug 12 '12 at 13:44
  • Thanks for this 1st answer mhusaini, but it doesn't give comma as decimal separator + I'm sure it's possible to have something simpler – nadir Aug 12 '12 at 13:53
  • I have updated the answer to account for comma as the decimal separator. Perhaps there is a simpler solution, but since you need to capture multiple sets of three digits and the first set can be less than three digits long, I am finding it hard to come up with a single regex that can do all of that. – mhusaini Aug 12 '12 at 14:22