You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript通配符匹配单词功能异常排查求助

JavaScript通配符匹配单词功能异常排查求助

你好呀!我看了你的代码和问题描述,马上就发现问题出在哪了——你现在的思路是把生成的字母加上星号拼成字符串,然后用includes()去检查字典数组里有没有完全等于这个带星号字符串的单词,但includes()是完全匹配,它只会找和wildGen一模一样的元素,而字典里的单词都是正常的英文单词,根本没有带星号的,所以当然永远返回“Invalid permutation”啦!

比如你说的i、t、a的情况,你生成的wildGen是*i*t*a*,而字典里的是dictionary,includes()只会判断数组里有没有*i*t*a*这个字符串,完全不会把星号当成通配符去匹配单词里的顺序字母。

那该怎么改呢?我们需要实现一个按顺序的通配符匹配逻辑,也就是检查字典里的某个单词是否能按顺序找到你生成的三个字母(中间可以有其他字符)。

给你两种修改方案:

方案一:用正则表达式实现通配符匹配

我们可以把生成的三个字母转换成正则表达式,比如把i、t、a转换成/.*i.*t.*a.*/,这样就能匹配任何包含i、t、a按顺序出现的字符串了。修改后的代码如下:

//define dictionary and alphabet
const dictionaryArray = ["alligator", "breadcrumb", "creation", "dictionary", "elephant", "farm", "general"];
const alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];

/*implement function to generate a random number. 
In conjunction with alphabet array, this will generate 
random letters */
function Random(maxNumber) {
    return Math.floor(Math.random() * maxNumber);
}

//generate random letters
var genLet1 = alphabet[Random(26)];
var genLet2 = alphabet[Random(26)];
var genLet3 = alphabet[Random(26)];

// 生成正则表达式:匹配任意字符(.*)分隔三个字母的情况
const regexPattern = new RegExp(`.*${genLet1}.*${genLet2}.*${genLet3}.*`);

function checkPermutationValidity() {
  // 检查字典里是否有单词符合正则表达式
  const isValid = dictionaryArray.some(word => regexPattern.test(word));
  if (isValid) {
    console.log("Valid permutation");
  } else {
    console.log("Invalid permutation");
  }
}
checkPermutationValidity();

方案二:手动遍历单词检查顺序

如果你不想用正则,也可以手动写一个函数,逐个检查单词里是否按顺序包含三个字母:

//define dictionary and alphabet
const dictionaryArray = ["alligator", "breadcrumb", "creation", "dictionary", "elephant", "farm", "general"];
const alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];

/*implement function to generate a random number. 
In conjunction with alphabet array, this will generate 
random letters */
function Random(maxNumber) {
    return Math.floor(Math.random() * maxNumber);
}

//generate random letters
var genLet1 = alphabet[Random(26)];
var genLet2 = alphabet[Random(26)];
var genLet3 = alphabet[Random(26)];

// 手动检查单词是否按顺序包含三个字母
function hasOrderedLetters(word, a, b, c) {
  let indexA = word.indexOf(a);
  if (indexA === -1) return false;
  let indexB = word.indexOf(b, indexA + 1);
  if (indexB === -1) return false;
  let indexC = word.indexOf(c, indexB + 1);
  return indexC !== -1;
}

function checkPermutationValidity() {
  const isValid = dictionaryArray.some(word => hasOrderedLetters(word, genLet1, genLet2, genLet3));
  if (isValid) {
    console.log("Valid permutation");
  } else {
    console.log("Invalid permutation");
  }
}
checkPermutationValidity();

这两种方案都能解决你的问题,比如当生成i、t、a的时候,都会匹配到dictionary,返回“Valid permutation”啦!

备注:内容来源于stack exchange,提问作者Maeve Convery

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.13 20:08:02