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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:14:44+00:00 2026-05-24T23:14:44+00:00

I want this code: function renderTemplate(temp,content){ for (var i in temp){ replace = new

  • 0

I want this code:

function renderTemplate(temp,content){ 
    for (var i in temp){
        replace = new RegExp("["+i+"]",'g');
        content = content.replace(i,temp[i]);
    }
    return content;
}
var temp = {'status':'ok','id':150};
var content = "your status is [status], again! your status is [status], and your id is [id], yes[id]";

alert(renderTemplate(temp,content));

To genrate me this string:

your status is ok, again! your status is ok, and your id is 150, yes 150

Instead, I get:

your ok is [status], again! your status is [status], and your 150 is [id], yes[id]

Look where the ok was placed….

you can run it here: http://jsfiddle.net/v9vzd/

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-24T23:14:46+00:00Added an answer on May 24, 2026 at 11:14 pm

    Although Adrian Lang’s fine answer should get you going, I would argue that you’re not taking the best approach. Compiling regexes from a variable can be awkward when it comes to escaping, and it’s generally slower performance-wise.

    If it were me, I would take advantage of passing a function to replace():

    function renderTemplate(temp, content) {
        return content.replace(/\[([^[\]+)\]/g, function ($0, key) {
            return temp[key];
        });
    }
    

    Working demo: http://jsfiddle.net/AKsHb/

    This works because the sub-expression capture, ([^\]]+) is passed to the replacing function as the second argument — labelled key in our function above — and this matches anything between a literal [ and ].

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

Sidebar

Related Questions

I want to convert function object to function. I wrote this code, but it
I'm trying to call a function that contains jQuery code. I want this function
I'm new to PHP and I want this code <option value= disabled=disabled>-------------</option> to be
i have this code: <script> $(#mod).change(function() { var matches=str.match(/(EE|[EJU]).*(D)/i); $.ajax({ type=post, url=process.php, data=matches, cache=false,
I have this code : function move() { $(document).mousemove(function(e){ var x = e.pageX; $('.linkHover').animate({'right':
When I run this code: function foo() { console.log(foo); var self = this; this.a
The code like this: $(.hide).click(function(){ $(.online-contact).animate({width: 'toggle',height: '100%'}, fast); $(this).hide(); $(.show-class).show(); }) I want
Why this code don't work,when i want run this code vwd 2008 express show
I want to add this code: <link rel=stylesheet type=text/css href=css/global.css /> <!--[if lt IE
I have this code in my controller and want to test this code line

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.