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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:15:51+00:00 2026-05-30T19:15:51+00:00

String.format = function() { var s = arguments[0]; for (var i = 0; i

  • 0
String.format = function()
{   var s = arguments[0];
    for (var i = 0; i < arguments.length - 1; i++)
    {   var reg = new RegExp("\\{" + i + "\\}", "gm");             
        s = s.replace(reg, arguments[i + 1]);
    }
  return s;
}

Please explain. Thanks.

  • 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-30T19:15:52+00:00Added an answer on May 30, 2026 at 7:15 pm

    It takes the first argument as a format string and replaces instances of {0} with the second argument, {1} with the third, and so on.

    String.format('{0} there, {1}', 'Hi', 'Josh'); => Hi there, Josh


    Going line by line:

    1. We’re attaching a function named format to the native String object (note, not to instances of strings. For that, you’d use String.prototype).
    2. arguments is a special object that is part of a function’s execution context (which also includes things like the value of this). It is array-like (it has keys that range from 0 to arguments.length-1) but it is not an Array (it is not an instance of Array and therefore does not have any of its prototype methods, like pop or push). The arguments object is how a JavaScript function can take an arbitrary number of parameters.
    3. Loop through each argument…
    4. Build a regular expression which matches { i } where i is the loop iteration number. The second argument is the regex options, gm enables global and multiline mode.
    5. Replace matches with the supplied value
    6.  
    7. Return the modified string.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a format string (?) I can pass to the VB6 Format function
I am trying to call Python's time.strftime() function using a Unicode format string: u'%d\u200f/%m\u200f/%Y
I have this function. The visual studio profile marked the line with string.Format as
i have this problem, doing: function GenGuid: String; var guid: TGuid; begin CreateGuid(Guid); Result
I'm trying to write a function that formats every (string) member/variable in an object,
string.Format() with it's bla {0} bla syntax is great. But sometimes I don't want
string format is always like this FirstName=ABC;LastName=XZY;Username=User1;Password=1234. I need the only UserName value (which
?string.Format(CultureInfo.GetCultureInfo(en-US), {0:d}, now) 4/12/2010 ?string.Format(CultureInfo.GetCultureInfo(fr-FR), {0:d}, now) 12/04/2010 I want to write a method:
Compare String.Format(Hello {0}, World); with Hello {0}.Format(World); Why did the .Net designers choose a
how string.format() can help to avoid using + in such statement: string statement =

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.