i have a question about splitting and resplacing substrings based on an array of starts and ends
i have this string saidogAngry Kappa saidogAngry saidogAngry ResidentSleeper ResidentSleeper
i need to replaces with “rep” parts of this string based on an array of this kind:
const eleArray = [
{
"start": "0",
"end": "10",
"rep": "63f5ce1593c142a09d714b34e1938990"
},
{
"start": "12",
"end": "16",
"rep": "25"
},
{
"start": "18",
"end": "28",
"rep": "63f5ce1593c142a09d714b34e1938990"
},
{
"start": "30",
"end": "40",
"rep": "63f5ce1593c142a09d714b34e1938990"
},
{
"start": "42",
"end": "56",
"rep": "245"
},
{
"start": "58",
"end": "72",
"rep": "245"
}
]
the problem is that after the first replace all other index are altered.
my initial idea was with split it in single sections and than remove/replace them
const msgArray = [];
let lastIndex = 0;
eleArray.forEach(ele => {
msgArray.push(message.slice(Number(lastIndex), Number(ele.start)));
// msgArray.push(message.slice(Number(ele.start), Number(ele.end+1)));
msgArray.push(ele.rep);
lastIndex = Number(ele.end)+1;
});
console.log(msgArray.join('');
there is a simplier way or a more optimized way? i need to have the best best performance out of it because i’m doing a text stream with a big quantity of messages.