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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:40:41+00:00 2026-06-15T18:40:41+00:00

In the following code, the alert(a) inside the JavaScript replace function will alert the

  • 0

In the following code, the alert(a) inside the JavaScript replace function will alert the matched strings, which, in this case, will be {name} and {place}.

This works as described by the documentation javascript docs , namely, the first argument to the function in the replace method will be the matched string. In the code below, the alert(b) will alert ‘name’ and ‘place’ but without the curly braces around them.

Why is that? How does it strip the curly braces for ‘b’? Here’s a fiddle http://jsfiddle.net/mjmitche/KeHdU/

Furthermore, looking at this example from the docs,

function replacer(match, p1, p2, p3, offset, string){
  // p1 is nondigits, p2 digits, and p3 non-alphanumerics
  return [p1, p2, p3].join(' - ');
};

Which of the parameters in this example would the ‘b’ in function(a,b) of the replace function below represent?

Part of my failure to understand might be due to the fact that I’m not sure what javascript does, for example, with the second parameter if the maximum number of arguments aren’t used.

code

var subObject = {
    name:  "world",
    place: "google"

}; 

var text = 'Hello, {name} welcome to {place}';


var replace = function (s, o) {
            return s.replace(/\{([^{}]*)\}/g,
              function (a, b) {
                  alert(a);
                  alert(b);
                var r = o[b];

                return typeof r === 'string' || typeof r === 'number' ? r : a;
              }
            );
}; 

var replacedText = replace(text, subObject); 
alert(replacedText); ​
  • 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-15T18:40:43+00:00Added an answer on June 15, 2026 at 6:40 pm

    The first parameter is the entire string matched by your regex (capturing groups don’t matter, so it becomes {name}).

    The second, third, fourth, etc. parameters are your capturing groups and since you only have one, your second argument becomes name.

    The last two parameters are the position of the match and the entire string. You can omit these arguments from your callback if you’d like.

    Here’s a slightly more readable version of your code that accounts for attributes that aren’t present in your replacement object:

    var replace = function(string, object) {
        return string.replace(/\{(.*?)\}/g, function(match, group) {
            return group in object ? object[group] : match;
        });
    };
    

    Demo: http://jsfiddle.net/KeHdU/4/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following JavaScript code which is inside a function which I am
I've got the following code: <code><html> <body><br> <script type=text/javascript src=http://code.jquery.com/jquery-1.4.2.min.js /> <script type=text/javascript> alert(debug);
Can anyone explain why the alert() in the following JavaScript code is firing? It
I have the following code: inside i put 2 alert() for debug. why does
Possible Duplicate: How can I get the name of function inside a JavaScript function?
I am try to write inside html document. this is my javascript code: <script
Friends I have following code in a javascript function that is assigned to onblur
Consider the following JavaScript code, meant to create an 8x8 table inside of a
The following code inside the tags gives error: Object Expected. <!-- JQuery/AJAX--> <script type=text/javascript>
I came across the following javascript: <body> <script type=text/javascript> (function() { alert('test'); })(); </script>

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.