string.replace with substitution by function
It may or may not be news to you that in Javascript you can do:
someString.replace( /\w+/g, function(match) { return "blah" } );Which in this case turns "the original string" into "blah blah blah". Your function is passed the match, and you return whatever you want. That's pretty handy, as you can run the match through transformations, or even use it to lookup or generate some entirely new value. You can also have side-effects from the match, which has interesting possibilities, not least of which is debugging:
function(match) { console.log("I matched %s", match); return match);
If you're like me, you might even think you could do something like this:
someString.replace( /<(\w+)[^>]*>)/g, function(tag) { var tagName = RegExp.$1; return tagName.toLowerCase(); // or whatever... } );.. but sadly you'd be wrong. At least as soon as you went to check in IE. It seems IE doesnt populate the properties from the match into the static RegExp object until after the whole replace is done. And your substitution function only gets passed the matched string, not the array of match + sub-matches you might be used to :( Still, there's plenty of mileage there. How about this:
function format(str, obj) { var str = str.replace(/\$\{[^}]+\}/g, function(mstr) { var key = mstr.substring(2, mstr.length-1); return (typeof obj[key] == "undefined") ? "" : obj[key]; }); return str; }.. which takes a string like
"<h1>${firstname} ${lastname}</h1>", and an object like: { firstname: "Bob", lastname: "Smith" }
to produce <h1>Bob Smith</h1>
Labels: javascript, workish
1 Comments:
Update: your replace function does get passed the submatches as arguments, in exactly the same way as the return 'matches' value has the full match as the first entry, and the submatches as the following entries. So you can do:
var data = {
'surname': 'Gummidge',
'firstname': 'Worzel'
};
"Surname:{surname}, First Name: {firstname}".replace(/\{([a-z])\}/g, function(matches, key) {
return data[name].toUpperCase();
});
...and get back Surname: GUMMDIGE, First Name: WORZEL
<< Home