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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:09:09+00:00 2026-06-05T14:09:09+00:00

I recently came across the Javascript supplant function by crockford. The function goes like

  • 0

I recently came across the Javascript supplant function by crockford. The function goes like this –

if (!String.prototype.supplant) {
    String.prototype.supplant = function (o) {
        return this.replace(/{([^{}]*)}/g,
            function (a, b) {
                var r = o[b];
                return typeof r === 'string' || typeof r === 'number' ? r : a;
            }
        );
    };
}

I need to understand the mechanics of this function and how exactly is this thing working. I have come across many explanations but all tend to make things complex by introducing technicalities that make things worse.

Trying to look for

1.The regex explanation here

2.the logic for the function(a,b) code block

3.the use of that return statement

  • 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-05T14:09:11+00:00Added an answer on June 5, 2026 at 2:09 pm

    as Tomalak said, spaces in regex are not trivial

    1. the regex basically matchs something like this: { ... }. the [^{}] means the content in the curly braces can be anything but curly nraces. The * means the length of the content can be zero or any number. The part inside the parentheses is Parenthesized Substring Matches

    2. The function passes a dictionary in as o, and performs a replace using the regex above. Whenever there is a match, the callback function of the replace will be invoked. a is the whole matched part, and b is the submatch part corresponding to the “Parenthesized Substring Matches”. The callback function looks for b as the key in the dictionary, and return the corresponding value

    3. the return statement means if the type of r is string or number, then return r; otherwise return a

    You can checkout the example code provided by Jared Farrish, which shows how the match and replace work very clearly.

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

Sidebar

Related Questions

I recently came across some code that looks something like this: <head> <?php /*
I recently came across this interview question (posted in a forum somehwere... looks like
I recently came across some code that looked like: if(sizeof(var,2) == 4) { ...
I recently came across a question about sequence points in C++ at this site,
I've been developing with PHP for some years now, and recently came across this
Recently I came across this error in my WCF trace: There was an error
Recently I came across this video on Programming Paradigms and the prof. used terms
I recently came across the includeMany jQuery plugin to include external JavaScript and CSS
I recently came across the following line of code: var type = (typeof x).toLowerCase();
I recently came across the pandas library for python, which according to this benchmark

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.