1

Possible Duplicate:
Format numbers in javascript

I have a string represents a number: "3507654" I need to split it on a parts, by 3, e.g "3 507 654"

Number can be any value, but always a number.

0

1 Answer 1

8

You could do the following:

"3507654".replace(/(\d)(?=(?:\d{3})+$)/g, "$1 ")

But this is not very efficient since it’s O(n2).

Sign up to request clarification or add additional context in comments.

5 Comments

nice answer. regex FTW.
… and there's a leading space if the number has 3n digits.
Real nice answer. Definitely +1
@Kenny: I was just about to mention how to fix that, but Gumbo updated his answer to include the fix already. +1.
thanks, very elegants and hackers :) +1

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.