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 8704423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:06:10+00:00 2026-06-13T03:06:10+00:00

Could you please explain why im not able to get values of backreferences from

  • 0

Could you please explain why im not able to get values of backreferences from a matched regex result and apply it some modification before effective replacement?

The expected result is replacing for example string ".coord('X','Y')" by "X * Y". But if X > to some value, divide this value by 2 and then use this new value in replacement.

Here the code im currently testing:

See /*>>1<<*/ & /*>>2<<*/ & /*>>3<<*/, this is where im stuck!

I would like to be able to apply modification on backrefrences before replacement depending of backreferences values.

Difference between /*>>2<<*/ & /*>>3<<*/ is just the self call anonymous function param

The method /*>>2<<*/ is the expected working solution as i can understand it. But strangely, the replacement is not working correctly, replacing by alias $1 * $2 and not by value…?

You can test the jsfiddle

//string to test
".coord('125','255')"

//array of regex pattern and replacement //just one for the example
//for this example, pattern matching alphanumerics is not necessary (only decimal in coord) but keep it as it
var regexes = [ //FORMAT is array of [PATTERN,REPLACEMENT]
    /*.coord("X","Y")*/ [/\.coord\(['"]([\w]+)['"],['"]?([\w:\.\\]+)['"]?\)/g, '$1 * $2']
                  ];
function testReg(inputText, $output) {
    //using regex
    for (var i = 0; i < regexes.length; i++) {
        /*==>**1**/ //this one works as usual but dont let me get backreferences values
         $output.val(inputText.replace(regexes[i][0], regexes[i][2]));

         /*==>**2**/ //this one should works as i understand it
         $output.val(inputText.replace(regexes[i][0], function(match, $1, $2, $3, $4) { 
            $1 = checkReplace(match, $1, $2, $3, $4);
            //here want using $1 modified value in replacement
            return regexes[i][3]; 
        }));

         /*==>**3**/ //this one is just a test by self call anonymous function
         $output.val(inputText.replace(regexes[i][0], function(match, $1, $2, $3, $4) {
            $1 = checkReplace(match, $1, $2, $3, $4);
            //here want using $1 modified value in replacement
            return regexes[i][4];
        }()));

        inputText = $output.val();
    }
}

function checkReplace(match, $1, $2, $3, $4) {
    console.log(match + ':::' + $1 + ':::' + $2 + ':::' + $3 + ':::' + $4);
    //HERE i should be able if lets say $1 > 200 divide it by 2
    //then returning $1 value
    if($1 > 200) $1 = parseInt($1 / 2);
    return $1; 
}​

Sure I’m missing something, but cannot get it!

Thanks for your help, regards.

EDIT WORKING METHOD:
Finally get it, as mentionned by Eric:

The key thing is that the function returns the literal text to
substitute, not a string which is parsed for backreferences.​​

JSFIDDLE

So complete working code: (please note as pattern replacement will change for each matched pattern and optimisation of speed code is not an issue here, i will keep it like that)

 $('#btn').click(function() {
    testReg($('#input').val(), $('#output'));
});

//array of regex pattern and replacement //just one for the example
var regexes = [ //FORMAT is array of [PATTERN,REPLACEMENT] /*.coord("X","Y")*/ 
    [/\.coord\(['"]([\w]+)['"],['"]?([\w:\.\\]+)['"]?\)/g, '$1 * $2']
                     ];

function testReg(inputText, $output) {
    //using regex
    for (var i = 0; i < regexes.length; i++) {
        $output.val(inputText.replace(regexes[i][0], function(match, $1, $2, $3, $4) {
            var checkedValues = checkReplace(match, $1, $2, $3, $4);
            $1 = checkedValues[0];
            $2 = checkedValues[1];
            regexes[i][1] = regexes[i][1].replace('$1', $1).replace('$2', $2);
            return  regexes[i][1];
        }));
        inputText = $output.val();
    }
}

function checkReplace(match, $1, $2, $3, $4) {
    console.log(match + ':::' + $1 + ':::' + $2 + ':::' + $3 + ':::' + $4);
    if ($1 > 200) $1 = parseInt($1 / 2);
    if ($2 > 200) $2 = parseInt($2 / 2);
    return [$1,$2];
}​
  • 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-06-13T03:06:11+00:00Added an answer on June 13, 2026 at 3:06 am
    .replace(regexes[i][0], function(match, $1, $2) {
        if ($1 > 200) $1 = parseInt($1 / 2);
        if ($2 > 200) $2 = parseInt($2 / 2);
        return $1 + "*" + $2;
    })); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please could someone explain to me why this regex does not match anything, when
Could someone please explain, I do not exactly get the concept. What is a
Could you please explain why this code is not syntactically correct? private void addEditor(final
Could someone please explain this weird phenomenon: http://jsfiddle.net/sPA2V/ For some reason, the box footer
could someone please explain to me the process of retrieving data from a remote
Wondering if someone could please explain the difference between these two queries and advise
Could someone please explain why this is happening? var y = new int[]{1,2}; Console.WriteLine(y
Could somebody please explain what this notation is in javascript? What is function(d) doing?
Could somebody please explain or link to a resource that will tell me why
Could someone please explain when would I want to use delegation instead of inheritance?

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.