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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:41:42+00:00 2026-05-26T14:41:42+00:00

I have a string with say: My Name is %NAME% and my age is

  • 0

I have a string with say: My Name is %NAME% and my age is %AGE%.

%XXX% are placeholders. We need to substitute values there from an object.

Object looks like: {"%NAME%":"Mike","%AGE%":"26","%EVENT%":"20"}

I need to parse the object and replace the string with corresponding values. So that final output will be:

My Name is Mike and my age is 26.

The whole thing has to be done either using pure javascript or jquery.

  • 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-26T14:41:42+00:00Added an answer on May 26, 2026 at 2:41 pm

    The requirements of the original question clearly couldn’t benefit from string interpolation, as it seems like it’s a runtime processing of arbitrary replacement keys.

    However, if you just had to do string interpolation, you can use:

    const str = `My name is ${replacements.name} and my age is ${replacements.age}.`
    

    Note the backticks delimiting the string, they are required.


    For an answer suiting the particular OP’s requirement, you could use String.prototype.replace() for the replacements.

    The following code will handle all matches and not touch ones without a replacement (so long as your replacement values are all strings, if not, see below).

    var replacements = {"%NAME%":"Mike","%AGE%":"26","%EVENT%":"20"},
        str = 'My Name is %NAME% and my age is %AGE%.';
    
    str = str.replace(/%\w+%/g, function(all) {
       return replacements[all] || all;
    });
    

    jsFiddle.

    If some of your replacements are not strings, be sure they exists in the object first. If you have a format like the example, i.e. wrapped in percentage signs, you can use the in operator to achieve this.

    jsFiddle.

    However, if your format doesn’t have a special format, i.e. any string, and your replacements object doesn’t have a null prototype, use Object.prototype.hasOwnProperty(), unless you can guarantee that none of your potential replaced substrings will clash with property names on the prototype.

    jsFiddle.

    Otherwise, if your replacement string was 'hasOwnProperty', you would get a resultant messed up string.

    jsFiddle.


    As a side note, you should be called replacements an Object, not an Array.

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

Sidebar

Related Questions

Let's say I have a string in the following form: myString={name, age, address, contacts,
Say I have a few string like Hi my name is <Name> Hi <name>,
Say I have a structure like: class SomeObject Public Name as String Public Created
Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do
Lets say I have class Person { public Person(int age, string name) { Age
Lets say I have an object class Person { public string Name { get;
Let's say I have a string like this: String test = hfikoebndolahsdHEL123LOkjahhsdqhuihs; And then
So say I have a string like so of a path $path = '/path/to/../../up/something.txt';
Say you define the following: class Person(name: String, age: Int) { def toXml =
Let's say I have a Parent object like this (in pseudo code) public Parent()

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.