JSFiddle - React, Tailwind, and code Playground

by guypursey

JavaScript

// An exploration of match and substitute process...

var rgx = /\((\d+)\)/,
    str = "this (0) a (1) sentence",
    sub = [
            "is",
            "test"
        ],
    mch,
    parsed = '',
    remainder = str,
    result;

while (mch = rgx.exec(remainder)) {
    console.log("match info: ", mch);
    parsed += remainder.substring(0, mch.index) + sub[mch[1]];
    remainder = remainder.substring(mch.index + mch[0].length);
}
result = parsed + remainder;

console.log(result);