如何构建一个不关心位置的正则表达式

发布于 2025-01-17 03:46:49 字数 384 浏览 5 评论 0原文

我正在用 HTML 和 Javascript 制作一个 Scrabble 游戏,我想添加一个基于玩家机架的“提示词”功能。 我有一个包含有效单词的字典数组,我需要根据玩家可用的图块来测试它们,但我不知道如何做一个正则表达式,它给我以下输出:

假设玩家有 DGOGI-*-E瓷砖和正则表达式应该返回:

doggies = True(因为通配符) 血腥=真实 狗=真 神=真 dodge = false(因为D只出现一次)

顺序并不重要,重要的是字母和字母重复的次数

我已经尝试使用 regexpal.com 中的 /[DGOGIE]/ 等有效字母来构建它但它根本不起作用 我也尝试玩这个 (?=.*a)(?=.*b)(?=.*c) 但还是没有结果

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怼怹恏 2025-01-24 03:46:49

我不太确定如何使用正则表达式执行此操作,但这是@Felix 描述的解决方案:

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"));

(抱歉,我回答得太晚了)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文