JSFiddle - React, Tailwind, and code Playground

by guypursey

JavaScript

// An exploration of match and substitute process, with lambda-replace...

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

result = str.replace(rgx, function() {
    return sub[arguments[1]];
});

console.log(result);