So, I have a string that is a series of numbers that range from 1-6. Depending on the number in question, I want to replace each with a different piece of code. This is for a Discord bot, so each of the six numbers should be replaced by a different Discord emoji code. I've tried using string.replace chains but, as these emoji codes contain numbers, the replacements start stacking on top of each other and replacing the replacements (That's a mouthful, I know!) which totally bugs out the code.
Here's an example chain:
1 2 1 4 6
And this is an index for how the numbers should be replaced:
1 = <:attr1:710526292784578581>
2 = <:attr2:710526292721664142>
3 = <:attr3:710526292642103398>
etc.
So it should turn out a string like this ideally, not one that ends up jumbled and mixed because the replacement chain is eating itself alive.
<:attr1:710526292784578581> <:attr2:710526292721664142> <:attr3:710526292642103398>
How can I make these replacements essentially all at once so that they only affect the items in the string, and don't start affecting each other? Answers would be much appreciated!
Edit: This was my own attempt with the replace chains which returns a buggy mess.
var attribEmoji = attribResult.join(" ").replace(/1/g, "<:attr1:710526292784578581>").replace(/2/g, "<:attr2:710526292721664142>").replace(/3/g, "<:attr3:710526292642103398>");
In the numerical chain, if every number has a space after it and the replacement numbers don't, then that is a simple problem to solve. Create an array of your icons and loop through them replacing the index with a space with the proper code.
var emojis = [];
emojis[1] = "<:attr1:710526292784578581>";
emojis[2] = "<:attr2:710526292721664142>";
emojis[3] = "<:attr3:710526292642103398>";
var chain = "1 2 3 ";
emojis.forEach(function(i,v){
chain = chain.replace(v + " ",i + " ");
});
console.log(chain);
You can do it something like this:
str = '1 2 1 4 6';
replacement = {
'1': '<:attr1:710526292784578581>',
'2': '<:attr2:710526292721664142>',
'3': '<:attr3:710526292642103398>',
'4': '<:attr4:710526292642103772>',
'5': '<:attr5:710526292642103008>',
'6': '<:attr6:710526292642105566>'
}
newStr = '';
for(var i = 0; i < str.length; i++) {
newStr += replacement[str.charAt(i)] ? replacement[str.charAt(i)] : str.charAt(i)
}
console.log(newStr)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With