Wow! Easy templates in Javascript
olav — Fri, 05/22/2009 - 16:56
Javascript really is a cute language. I just discovered, or rather, have read it in Mark Birbeck’s post that the Javascript replace function can take a function as section parameter.
Here is my go at an implementation of the strtr() function of PHP in Javascript – neat!
function strtr(text, obj) {
return text.replace(/§(\w+)/g, function(m) {
if (typeof(obj[RegExp.$1]) == 'undefined') {
return m;
}
else {
return obj[RegExp.$1];
}
});
}
This will replace all occurences of §bla in text with an object property obj.bla.




Kommentiert
1 week 3 days ago
5 weeks 5 days ago
26 weeks 16 hours ago
27 weeks 4 days ago
27 weeks 5 days ago
31 weeks 3 days ago
1 year 11 weeks ago
1 year 34 weeks ago
1 year 42 weeks ago
2 years 1 week ago