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”。原來,原文中定義的正則表達式能夠驗證的數(shù)字范圍是...原來如此啊,哈哈,樓豬心虛的笑了。