eric d. Hi, thanks for that brilliant article. Made a lot of things a lot clearer! Note: new RegExp("%([1-" + arguments.length + "])", "g"); will fail passed 9 arguments (the regexp would be "%([1-10])" so it will only match %0 and %1).
I think an easy fix would be something like: function format(string) { var args = arguments; var pattern = new RegExp("%([0-9]+)", "g"); return String(string).replace(pattern, function(match, index) { if (index == 0 || index >= args.length) throw "Invalid index in format string"; return args[index]; }); }; (Sorry for nitpicking, I understand it was only an example and brevety is the main objective, but its a great function to have)
Posted on: January 20th 2009, 12:01 am
这个留言的家伙给足了作者面子,称“I understand it was only an example and brevety is the main objective, but its a great function to have”。原来,原文中定义的正则表达式能够验证的数字范围是...原来如此啊,哈哈,楼猪心虚的笑了。