Recently, I started to make a word game which involves making words out of the letters of another word. For example, if you have the word “Teutonic”, you could make “tonic”, “tone”, etc. In the game, you can only use the available letters in the word, and not repeat them. That means that while “tonic” would be valid in the above example, “tonico” would not because there is only one “o” in “Teutonic”.
My question is basically, how can I make a regular expression so that it searches for one instance of the letter and remembers it so that, given the same letter later in the loop, doesn’t look for it again?
So if you have “Teutonic” and you are searching using a regex for each letter in “tonico” (“t”,”o”,”n”,”i”,”c”,”o”), you would get “true” for every one, except for the last “o”.
If you have the word “Pencil”, you would get true for “pen”, “line”, and “pile”, but not from “Pipe”.
Or if you have “small”, you would get true for “mall”, “malls” but not “mass”.
Edit:
Thank you very much for all of your prompt replies!
I suspected that it wasn’t going to be possible with a regex approach, but I wanted to make sure. Instead, I experimented a bit with the array method, thought I didn’t get very far.
Thanks again guys!
Split the string to array, and then compare with array.