Javascript用子字符串搜索匹配两个数组

标签 javascript arrays

您好,非常感谢您为此提供的所有帮助。我有两个数组,第一个是没有扩展名的文件名,第二个包含扩展名。我需要匹配文件名并输出匹配项的第三个数组。

ArrayFileName = [one, two, three, three, five, six, ten]
ArrayFileNameWExt = [one.txt, two.txt, three.txt, ten.wmf, eleven.cgm]

输出数组为

NewArrayFileNameWExt = [one.txt, two.txt, three.txt, ten.wmf]

感谢大家的帮助。 玛克辛

最佳答案

这可以通过 filter() 轻松完成在第二个数组上并使用正则表达式来测试每个 filename 是否包含在第一个数组中:

ArrayFileName = [
  "one", "two", "three", "three", "five", "six", "ten", "dot.dot"
];

ArrayFileNameWExt = [
  "one.txt",
  "two.txt",
  "three.txt",
  "ten.wmf",
  "eleven.cgm",
  "dot.dot.csv",
  "foo",
  ".",
  ""
];

let res = ArrayFileNameWExt.filter(x =>
{
    let matchs = x.match(/(.*)\.(.*)$/);
    return ArrayFileName.includes(matchs && matchs[1]);
});

console.log(res);

关于Javascript用子字符串搜索匹配两个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54225369/

相关文章:

javascript - googleapis 失败(在 JWT Auth 之后),出现 TypeError : authClient. request is not a function

arrays - 如何使用加入?

javascript - 壁纸.js 不工作

javascript - 动态创建的元素上的事件绑定(bind)?

javascript - 未解析时为 undefined json,解析时为 undefined token

javascript - 存储来自随机文本/颜色生成器的响应,比较它们并存储在数组中

php - 正则表达式散列和冒号

php - Codeigniter:将查询结果存入关联数组

PHP : Remove object from array

javascript - 如何防止从 Web 浏览器中的登录页面保存凭据