JSFiddle - React, Tailwind, and code Playground

by blesh

JavaScript

function format(input, obj) {
    var tokenReg = /[^\\]{0,1}{(\\{|\\}|.*?[^\\])}/g,
        output = '',
        i = 0,
        argsLen = arguments.length,
        m;
    if (argsLen > 2) {
        obj = [].slice.call(arguments, 1);
    } else if (argsLen === 2 && typeof obj !== 'object') {
        obj = [obj];
    } else if (argsLen === 1) {
        obj = {};
    }
    while (m = tokenReg.exec(input)) {
        output += input.substring(i, m.index + m[0].length - m[1].length - 2);
        output += obj[m[1]] || '';
        i = m.index + m[0].length;
    }
    return output;
}


var result4 = format("{0} {1} {2} {3} {0} {1} {2} {3} {0} {1} {2} {3}");

console.log(result4);