Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 509489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:00:58+00:00 2026-05-13T07:00:58+00:00

Is it possible to do number grouping (e.g., converting the number 1000 to the

  • 0

Is it possible to do number grouping (e.g., converting the number 1000 to the string "1 000") using one pass with only regular expressions? (I know the boundary between regexp and language facilities are a bit blurred in some systems – listen to your conscience before replying.)

Reason why I’m asking: Another dev recently asked me how to do number grouping in JavaScript, and showed me a slightly incorrect JavaScript function using regexps. I gave him a better alternative, but his regexp nagged at me because this kind of rewrite is definitely something a regular grammar should be able to do, but I really can’t figure out how to write a regexp for it.

This is my first naïve attempt, which I knew would be incorrect:

function group(n) { return n.toString().replace(/(\d{3})/g, "$1 "); }

This approach has two flaws; group(1000) yields "100 0", and group(100) yields "100 " (trailing space). You can fix it this way:

String.prototype.reverse = function () { 
    var a = []; 
    for (var i = this.length; i >= 0; --i) a.push(this[i]); 
    return a.join("");
}; 
function group(n) { 
    return n.toString().reverse().replace(/(\d{3})/g, "$1 ").
        trimRight().reverse(); 
}

But this requires not one, not two, not even three but FOUR passes (two reverses, one replace, and trimRight)! I then ventured out into look-behind land, and came up with:

function group(n) { return n.toString().replace(/(\d{3}(?!\d))/g, " $1");

… which doesn’t work at all (edit – probably because I confused look-behind and negative look-ahead…) – it only matches the last three digits (group(1000000000) becomes "1000000 000"). Look-ahead works a little better:

function group(n) { return n.toString().replace(/(\d{3})(?=\d)/g, "$1 "); }

Which more or less brings me back where I started – I’m rid of the trailing space, but group(1000) still yields "100 0".

So – can this be done with a single regexp replace pass? I’m language agnostic, as this should only need to use regexp facilities.

Note: This is not a question about how to do localization, and I’m not engaging in premature optimization. I’m just curious as to whether this is possible, and if it isn’t, why not.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:00:58+00:00Added an answer on May 13, 2026 at 7:00 am

    Here’s a version that will work in JavaScript:

    return n.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1 ");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Regular expression where part of string must be number between 0-100 I'd
Possible Duplicate: How would you set a variable to the largest number possible in
Possible Duplicates: How would you set a variable to the largest number possible in
Possible Duplicate: How to check if a number is a power of 2 I
Possible Duplicate: A comprehensive regex for phone number validation Just a simple regex required
Possible Duplicate: How to limit number of characters per line in text area to
Possible Duplicates: C - determine if a number is prime Is there any way
Is it possible to find the number of lines of code in an entire
Is it possible to overload the action methods based on number of parameters in
I wonder if it's possible to get the number of a <a> tag when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.