How do I build a Regexp that doesn't care about positions

62 Views Asked by At

I'm making a Scrabble game with HTML and Javascript and I'd like to add a "hint word" feature based on the player's rack. I have a dictionary array with the valid words, and I need to test them against the players available tiles but I couldn't figure out how to do a regexp that gives me the following output:

Lets say the player has D-G-O-G-I-*-E tiles and the Regexp should return:

doggies = True (because of the wildcard) gore = True dog = True god = true dodge = false (because D appears only once)

It doesn't matter the order, only the letters and the times that letter is repeated

I've already tried building it with the valid letters like this /[DGOGIE]/ in regexpal.com but it didn't work at all I also tried playing with this (?=.*a)(?=.*b)(?=.*c) but still no result

1

There are 1 best solutions below

0
On

I'm not quite sure how you would do this with regex, but here is the solution that @Felix described:

function check(letters, wordToCheck) {
    for (char of wordToCheck) {
        if (letters.split(char).length - 1 < wordToCheck.split(char).length - 1) {
            return false;
        }
    }
    return true;
}

console.log(check("dgogyefi", "doggie"));
console.log(check("gepdoi", "doggie"));

(Sorry if I answered too late)