尝试匹配多个字符

Trying to match on multiple characters

已经尝试了各种方法,但没有一个是有效的...

我有以下代码:

        correctList(id, e) {
          if (this.regex.test(e.key)) {
            this.correctLetters.push([e.key, id]);
          }

          this.correctLetters.forEach(
            (array) => {
              if (this.filteredWords.length > 1) {
                this.filteredList = this.filteredWords.filter((word) => word.indexOf(array[0]) === array[1]);
            } else {
                this.filteredList = this.FiveLetterWords.filter((word) => word.indexOf(array[0]) === array[1]);
            }
            this.setFilteredWords(this.filteredList);
            }
          );
        },

其中 'this.correctLetters[]' 是:

[[e,0], [l,1], [d,2]...]

这是使用输入构建的,输入的顺序是索引号(数组中的 0、1、2)

'this.filteredWords' 是:

["cigar", "rebut", "sissy", "humph", "awake", "blush",  "focal", "evade", "naval", "serve", "heath", "dwarf", "model", "karma", "stink", "grade", "quiet", "bench", "abate", "feign", "major", "death", "fresh", "crust", "stool", "colon", "abase", "marry", "react", "batty", "pride", "floss", "helix", "croak", "staff", "paper", "unfed", "whelp", "trawl", "outdo", "adobe", "crazy", "sower", "repay", "digit", "crate", "cluck", "spike", "mimic", "elder".....]

还有很多,但与列出所有问题并不真正相关

我的问题是过滤器...以单词“elder”为例...如果我输入“e”,“l”,“d”,然后我将按以“e”开头的单词过滤列表,然后按“[=] 开头的单词过滤38=]el”然后过滤但单词盯着“eld”。但只要我输入“elde”,它将数组过滤为空(由于我目前的过滤方法,我知道这在技术上是正确的)然后如果我输入“elder”,我将得到所有以“[=38”结尾的单词=]r".

所以基本上我明白我的问题是 indexOf(() 部分,因为它只匹配单词中字母的第一个实例,但我想过滤字母 === 的单词该字母的索引,但我不确定如何更改。因此,如果我立即键入 E R,我只会以 R 词结尾,我想要任何以 ER 结尾的词,包括任何以 E 开头的词,或者有另一个 E在那里...

希望这是有道理的,我正在绕圈子......有人建议正则表达式,但我最初的几次尝试没有帮助,但可能做错了......

编辑: 用以下方法修复它:

correctList(id, e) {
    if (this.regex.test(e.key)) {
    this.correctLetters.push([e.key, id]);
    }

    this.correctLetters.forEach(
    (array) => {
        if (this.filteredWords.length > 1) {
        this.filteredList = this.filteredWords.filter(
            (word) => {
                const wordSplit = word.split('');
                return wordSplit[array[1]] === array[0];
            }
        );
    } else {
        this.filteredList = this.FiveLetterWords.filter(
            (word) => {
                const wordSplit = word.split('');
                return wordSplit[array[1]] === array[0];
            }
        );
    }
    console.log(this.filteredList);
    this.setFilteredWords(this.filteredList);
    }
    );

但是不确定有没有更优雅的方法...

我可能完全忽略了这里的要点,但看起来您只是在尝试过滤包含用户在某处拥有的内容的单词列表 typed/selected...。在这种情况下,它是简单

const words = filteredWords.filter(x => x.indexOf(something) > -1);

下面是一个例子,(开始输入 er 你会在某处得到任何带有“er”的词):

const filteredWords = ["cigar", "rebut", "sissy", "humph", "awake", "blush",  "focal", "evade", "naval", "serve", "heath", "dwarf", "model", "karma", "stink", "grade", "quiet", "bench", "abate", "feign", "major", "death", "fresh", "crust", "stool", "colon", "abase", "marry", "react", "batty", "pride", "floss", "helix", "croak", "staff", "paper", "unfed", "whelp", "trawl", "outdo", "adobe", "crazy", "sower", "repay", "digit", "crate", "cluck", "spike", "mimic", "elder"]

document.getElementById("entry").addEventListener("input", e => {
  const words = filteredWords.filter(x => x.indexOf(e.target.value)>-1);
  document.getElementById("output").value = words.join("\r\n")
})
<input id="entry" type="text">
<br>
<textarea id="output" rows="10"></textarea>

要获得在给定位置具有给定字母的单词集,只需这样做...

filteredList = FiveLetterWords.filter(
    word => correctLetters.reduce(
        (prev, [letter, index]) => prev && (word[index] === letter),
        true
    )
);