Sorry for the confusing / misleading title, wasn 't really sure how to word this one.
Basically, I am pulling players data from a CS:GO server, importantly the players rank which is given to me as a string such as {default}[{pink}Master{default}]
In-game colours inside of the curly braces get replaced with hex values, however for my react app I am trying to replace them so it'
s like< span style = "color: #fff;" > [ < /span><span style="color: #ffb6c1;">Master</span > <span style="color: #fff;">]</span>
or[Master] where master would be some pink shade
I can replace the {
colours
}
with the opening span tag easily, however I 'm not sure how to get where the span should end.
Tried something like this
const replaceColours = function(str) {
const original = str;
let index;
let indexNext;
let strOut;
let colour;
if (str.indexOf("{default}") !== -1) {
index = str.indexOf("{default}");
// console.log(index);
strOut = str.substr(index);
strOut = strOut.replace("{default}", "");
// console.log(strOut);
indexNext = strOut.indexOf("{");
if (indexNext !== -1) {
strOut = strOut.substr(0, indexNext);
}
// console.log(strOut);
colour = reactStringReplace(str, "{default}", (match, i) => (
<span key={i} style={{color: '#fff'}}></span>
));
}
console.log(colour);
return colour;
}
reactStringReplace is from https://www.npmjs.com/package/react-string-replace
My idea was to get the index of the first match {colour} then the index of the next { and get whatever is in-between those two.
Obviously doesn't work properly, but any help would be great.