I have a string of text with 100 different "keys" between some characters « »
and an array containing the UsedKeys
. I'm trying to compare the string str
to the array and replace all keys found in the UsedKey
array with check mark character. I'm still very new to loops as well as javascript in general, and I can't seem to get the script to replace any of the keys except for the last one in the array. In this case that would be «SPLXVIII»
.
I also have a second array called AllKeys
which I'd like to use in order to hide all of the non-used keys. I'm certain I'd have to find the difference between the two arrays (UsedKeys
and AllKeys
), i'm just not quite sure how.
Questions:
1) How can I replace all of the keys in the UsedKey array?
2) How can I hide all of the other keys in the string that aren't in the UsedKey array?
function replace() {
var str = ("«SPII»
You can turn the
UsedKeys
array into a single regexp that will replace all of them, using the|
alternative operator. Theg
modifier makes it replace all occurrences.You can then hide all the rest with:
You should use a regular expression.
The 'g' means globally. So, replace all the occurences.