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.
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;
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.