January 2011
1 post
2 tags
Syntactic sugar: RexExp.exec with callbacks
The RegExp.exec method returns an array of results when there’s a match or null when there’s none. I often find myself coding the same check over and over again:
var result = /(.+)=(.+)/.exec("name=value");
if (result) {
console.log(result[1] + " = " + result[2]);
}
It would make life easier if exec accepted a callback method as the second argument (and then a scope as the...