JSFiddle - React, Tailwind, and code Playground

JavaScript

function getIndicesOf(searchStr, str, caseSensitive) {
    var startIndex = 0, searchStrLen = searchStr.length;
    var index, indices = [];
    if (!caseSensitive) {
        str = str.toLowerCase();
        searchStr = searchStr.toLowerCase();
     }
     while ((index = str.indexOf(searchStr, startIndex)) > -1) {
            indices.push(index);
            startIndex = index + searchStrLen;
     }
     return indices;
}

function replaceCharacter(text ,char, withChar, index) {
    var indices = getIndicesOf(char,text, true);
    return text.substr(0, indices[index-1]) + withChar +     text.substr(indices[index-1]+withChar.length);
    console.log(text);
}
var text = 'Aekwwewj fkjkwefhwA wkejqwkjeqkwjeA A ewqeqw';

// text is the full text where the subsitution will be performed
// replaceCharacter(text, char, witchar, index)
text = replaceCharacter(text, 'A', 'W', 2);