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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:34:15+00:00 2026-06-15T09:34:15+00:00

I would like a javascript function that mimics the python .format() function that works

  • 0

I would like a javascript function that mimics the python .format() function that works like

.format(*args, **kwargs)

A previous question gives a possible (but not complete) solution for ‘.format(*args)

JavaScript equivalent to printf/string.format

I would like to be able to do

"hello {} and {}".format("you", "bob"
==> hello you and bob

"hello {0} and {1}".format("you", "bob")
==> hello you and bob

"hello {0} and {1} and {a}".format("you", "bob",a="mary")
==> hello you and bob and mary

"hello {0} and {1} and {a} and {2}".format("you", "bob","jill",a="mary")
==> hello you and bob and mary and jill

I realize that’s a tall order, but maybe somewhere out there is a complete (or at least partial) solution that includes keyword arguments as well.

Oh, and I hear AJAX and JQuery possibly have methods for this, but I would like to be able to do it without all that overhead.

In particular, I would like to be able to use it with a script for a google doc.

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-06-15T09:34:17+00:00Added an answer on June 15, 2026 at 9:34 am

    UPDATE: If you’re using ES6, template strings work very similarly to String.format: https://developers.google.com/web/updates/2015/01/ES6-Template-Strings

    If not, the below works for all the cases above, with a very similar syntax to python’s String.format method. Test cases below.

    String.prototype.format = function() {
      var args = arguments;
      this.unkeyed_index = 0;
      return this.replace(/\{(\w*)\}/g, function(match, key) { 
        if (key === '') {
          key = this.unkeyed_index;
          this.unkeyed_index++
        }
        if (key == +key) {
          return args[key] !== 'undefined'
          ? args[key]
          : match;
        } else {
          for (var i = 0; i < args.length; i++) {
            if (typeof args[i] === 'object' && typeof args[i][key] !== 'undefined') {
              return args[i][key];
            }
          }
          return match;
        }
      }.bind(this));
    };
    
    // Run some tests
    $('#tests')
      .append(
        "hello {} and {}<br />".format("you", "bob")
      )
      .append(
        "hello {0} and {1}<br />".format("you", "bob")
      )
      .append(
        "hello {0} and {1} and {a}<br />".format("you", "bob", {a:"mary"})
      )
      .append(
        "hello {0} and {1} and {a} and {2}<br />".format("you", "bob", "jill", {a:"mary"})
      );
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="tests"></div>
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a javascript function that works as I would like : <script language=javascript
I would like to write a javascript function that works something like this... f([[a]],
Possible Duplicate: JavaScript: Why the anonymous function wrapper? I would like to ask you
I would like to write a JavaScript function that validates a zip code, by
I have an iframe and inside that iframe a javascript function. I would like
I would like to a regular expression to use with javascript replace function that
I have a javascript function that I would like to execute after every asynchronous
I have a javascript function that returns an array. I would like to know
I would like a JavaScript function to have optional arguments which I set a
Hovering the mouse over an element flips it. I would like a JavaScript function

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.