function formatString(string, params) {
return string.replace(/{(\d+)}/g, (match, index) => {
return typeof params[index] !== 'undefined' ? params[index] : match;
});
}
var name = "Alex";
var age = 30;
var formattedMsg = formatString("Hi, {0}! You are {1} years old.", [name, age]);
console.log(formattedMsg);
function formatString(string, params) { return string.replace(/{(\d+)}/g, (match, index) => { return typeof params[index] !== 'undefined' ? params[index] : match; }); } var name = "Alex"; var age = 30; var formattedMsg = formatString("Hi, {0}! You are {1} years old.", [name, age]); console.log(formattedMsg);