Interpolation is just a fancy word for variable substitutioin in a string.
According to the free dictionary interpolate means:
1. To insert or introduce between other elements or parts.
2. a. To insert (material) into a text.
Perl does this very well, other language less so. Less is a hybrid of the C# method for string formatting. I find it pretty convenient when creating a lot of dynamic html to insert using that oh so hander innerHTML property. Remember, innerHTML is usually the fastest way to add dynamic html.
function format(format) {
var params = arguments;
var toReturn = format;
for (var i = 0; i < params.length - 1; i++) {
var rString = '\\$\\{' + (i) + '\\}';
var regex = new RegExp(rString, "g");
toReturn = toReturn.replace(regex, params[i + 1]);
}
return toReturn;
}
Here's the
original interpolation function that extended the string object. I don't really think that extending builtin objects is a great idea unless there is no other way.