JSFiddle - React, Tailwind, and code Playground
JavaScript
console.clear();
let attemptPhrase = "one two three one"
let indexToReplace = 2; // CHANGE THIS TO 1 OR 2
let i = 0;
function replacer(match, offset, string) {
i += 1
if( indexToReplace === i){
return "(" + match + ")"
}
return match
}
attemptPhrase = attemptPhrase.replace(/\bone\b/gi, replacer );
console.log(attemptPhrase);