function string_format(f, ...a) { if (!f.match(/^(?:(?:(?:[^{}]|(?:\{\{)|(?:\}\}))+)|(?:\{[0-9]+\}))+$/)) { throw new Error("Invalid format string."); } return f.replace(/((?:[^{}]|(?:\{\{)|(?:\}\}))+)|(?:\{([0-9]+)\})/g, (m, s, i) => { if (s) { return s.replace(/(?:{{)|(?:}})/g, m => m[0]); } else { if (i >= a.length) { throw new Error("Argument index is out of range in format."); } return a[i]; } }); }