0

I am trying to replace the 'parameters' in my string with the actual values of the parameters using the replace() method, but for some reason I cannot get it it to work. The string I am using is:

var temp = "This {{application}} will be down from {{start}} to {{finish}}."

I want to replace {{application}} with the application name, and so on.

var regEx = /{{(.*?)}}/;

This is the regex I use to grab the values between the brackets and that part works. Here is the rest of my code:

if (regEx.exec(temp)[1] === "application") {
     temp.replace(regEx, params.value);
}

'params.value' is the name of the application. I thought this would work, but it is not.

4
  • 1
    why not just use simple plain replace? jsfiddle.net/th6crbbm Commented Feb 2, 2016 at 13:34
  • I tried to. For some reason it is not working. Commented Feb 2, 2016 at 13:41
  • and the solution is to switch to regex?!?! Commented Feb 2, 2016 at 13:42
  • you know, you are missing a semicolon at the end on your var temp =...... maybe thats why replace doesnt work. Commented Feb 2, 2016 at 13:45

3 Answers 3

1

To replace only a single string(static)

var appName = "application"; // String to replace
var regex = new RegExp("{{" + appName + "}}", "g"); // Use `g` flag to replace all occurrences of `{{application}}`
temp = temp.replace(regex, param.value);

var appName = "application",
    regex = new RegExp("{{" + appName + "}}", "g"),
    temp = "This {{application}} will be down from {{start}} to {{finish}}.";

var param = {
    value: 'StackOverflow'
};
temp = temp.replace(regex, param.value);

console.log(temp);
document.body.innerHTML = temp;


To replace all the strings inside brackets by their respective values(Dynamic)

You can use String#replace with an object to replace values.

var regex = /{{(.*?)}}/g;
// Match all the strings in the `{{` and `}}`
// And put the value without brackets in captured group

temp = temp.replace(regex, function(m, firstGroup) {
    // m: Complete string i.e. `{{foobar}}`
    // firstGroup: The string inside the brackets

    return params[firstGroup] || 'Value not found in params';
    // If the value for the key exists in the `params` object
    //     replace the string by that value
    // else
    //     replace by some default string
});

var params = {
    application: 'Stack Overflow',
    start: 'On Sunrise',
    finish: 'In Dreams'
};

var temp = "This {{application}} will be down from {{start}} to {{finish}}.";

var regex = /{{(.*?)}}/g;
temp = temp.replace(regex, function(m, firstGroup) {
    return params[firstGroup] || 'Value not found in params';
});

console.log(temp);
document.body.innerHTML = temp;

Sign up to request clarification or add additional context in comments.

Comments

1

Here is an helper function to produce what you expect

function replace(str, dict) {
    return str.replace(/{{(.*?)}}/g, function(match, $1) {
        return dict[$1] || match;
    });
}

replace(
    "This {{application}} will be down from {{start}} to {{finish}}.",
    {
        'application': 'pigeon',
        'start': '8am',
        'finish': '9pm'
    }
);
// "This pigeon will be down from 8am to 9pm."

This will accept a mapping of values to replace and the replacement. And returns the string correctly formatted.

2 Comments

This will only replace the first occurrence of each marker.
You are right ! So yeah ... regex are mandatory (or an ugly loop) :). I edit for a regex.
0

You can use a callback inside a replace to evaluate the capture group contents:

var name = "Google";
var temp = "This {{application}} will be down from {{start}} to {{finish}}.";
var res = temp.replace(/{{(.*?)}}/g, function (m, g) {
  return g === "application" ? name : m;
});
document.body.innerHTML = res;

Here, m is the whole matched text with braces and g is the submatch without braces.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.