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

  • SEARCH
  • Home
  • 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 7542197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:06:08+00:00 2026-05-30T08:06:08+00:00

I am trying to create a regular expression with a character class that has

  • 0

I am trying to create a regular expression with a character class that has a specific quantifier which is a variable for example:

var str = "1234.00";
var quantifier = 3;
str = str.replace(/(\d)(\d{quantifier}\.)/,"$1,$2");
//str should be "1,234.00"

This works as follows (without a variable):

var str = "1234.00";
str = str.replace(/(\d)(\d{3}\.)/,"$1,$2");
//str == "1,234.00"

However it does not have the same functionality with a quoted pattern instead of a slash-delimited pattern as follows:

var str = "1234.00";
str = str.replace("(\d)(\d{3}\.)","$1,$2");
//str == "1234.00" - not "1,234.00"
//quote symbol choice does not change this
str = str.replace('(\d)(\d{3}\.)',"$1,$2");
//str == "1234.00" - not "1,234.00"

edit: to be more clear I have added a summary question which was answered below:
How do I create a regular expression with an interpolated variable from a quoted string?

Although my preference would be to use interpolation, it seems that is not available (at least in this context), and is not necessary.

I have also tried to come up with a way to concatenate/join some regex literals to achieve the same result, but have been unable to do so for this use case.

As a side note – I am familiar with this type of regular expression in perl:

my $str = "1234.00";
my $quantifier = 3;
$str =~ s/(\d)(\d{$quantifier}\.)/$1,$2/;
# $str eq "1,234.00"

Which can be made useful as follows:

my $str = "1234567890.00";
for my $quantifier (qw(9 6 3)) {
    $str =~ s/(\d)(\d{$quantifier}\.)/$1,$2/;
}
# $str eq "1,234,567,890.00"

With the suggestions/answers provided I have created a sample currency string prototype as follows:

String.prototype.toCurrency = function() {
    var copy = parseFloat(this).toFixed(2);
    for (var times = parseInt(copy.length/3); times > 0; times--) {
        var digits = times * 3;
        var re = new RegExp("(\\d)(\\d{" + digits + "}\\.)");
        copy = copy.replace(re,"$1,$2");
    }
    return '$'+copy;
};
str = "1234567890";
str.toCurrency();
// returns "$1,234,567,890.00"
  • 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-30T08:06:10+00:00Added an answer on May 30, 2026 at 8:06 am

    There are two problems with this statement:

    str.replace("(\d)(\d{3}\.)","$1,$2");
    

    The first is that you are passing a string and not a regular expression object, and the second is that within a string literal the backslash has a special meaning to escape certain things (e.g., "\n" is a newline) so to have an actual backslash in your string literal you need to double it as "\\". Using the RegExp() constructor to create a regex object from a string you get this:

    str.replace(new RegExp("(\\d)(\\d{3}\\.)"),"$1,$2");
    

    So from there you can do this:

    var quantifier = 3
    str = str.replace(new RegExp("(\\d)(\\d{" + quantifier + "}\\.)"),"$1,$2");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a CSS editor and am trying to create a regular expression that
I'm no expert on Regex. I'm trying to create a regular expression that will
this one is really easy. I'm trying to create a Regular Expression that will
I am trying to create a regular expression in C# that allows only alphanumeric
I am trying to create a JavaScript regular expression that can find a keyword
I'm trying to create regular expression that filters from the following partial text: amd64
I am trying to create a regular expression which will look for a email
I am trying to create a regular expression that understands mathematical equations (>, <,
I'm trying to create a regular expression to find all lines that contain a
I was trying to create a regular expression which validate !! If the text

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.