- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我们的字符串:
const a = "your majesty they are ready";
const b = "your highness they are ready";
我想得到一个包含两个字符串之间最长精确匹配的数组。
[ ["your"], ["they are ready"] ]
不是 这个得到最短匹配的结果:
[ ["your"], ["they"], ["are"], ["ready"] ]
获得单词匹配对我来说是一件容易的事。但我需要一个人来找出一个合适的解决方案,以获得尽可能长的精确匹配......
// exactMatch("I was sent","sent") returns true And exactMatch("I was sentt","sent") returns false
function exactMatch(ref,compare) {
return new RegExp("\\b" + ref + "\\b").test(compare);
}
备注 :我不想要最长的匹配...我想要所有可能的最长匹配。
最佳答案
像这样的东西:
function exactMatch(a, b) {
a = a.split(' ');
b = b.split(' ');
let s = [];
let out = [];
let x = a.map(x => b.includes(x) ? x : null);
x.forEach((v) => {
if (v == null) {
out.push(s);
s = [];
} else {
s.push(v);
}
});
out.push(s);
out = out.map(x => x.join(' ')).filter(x => x);
return out;
}
let a = "your majesty they are ready";
let b = "your highness they are ready";
let c = exactMatch(a, b);
console.log(c);
a = "your majesty they are ready, but this is another test";
b = "your highness they are ready, yet this is another test";
c = exactMatch(a, b);
console.log(c);
a = "hello your majesty they are ready, but this is another test";
b = "hi your highness they are ready, yet this is another test";
c = exactMatch(a, b);
console.log(c);
a = "hello your majesty they are ready, but this is another test made";
b = "hi your highness they are ready, yet this is another test completed";
c = exactMatch(a, b);
console.log(c);
关于javascript - 获取两个字符串之间最长的完美匹配javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64225199/
我正在尝试编写一个名为 map-longest 的 Clojure 实用函数(感谢备用名称建议)。该函数将具有以下“签名”: (map-longest fun missing-value-seq c1
为什么我创建了一个重复的线程 我在阅读后创建了这个线程 Longest increasing subsequence with K exceptions allowed .我意识到提出问题的人并没有真
我正在编写一个 Sub 来识别 1 到 1000 之间最长的 Collatzs 序列。由于我刚刚开始学习 VBA,我想知道如何添加过程来计算每个序列的长度。 Sub Collatz() Dim i
我正在编写一个 Sub 来识别 1 到 1000 之间最长的 Collatzs 序列。由于我刚刚开始学习 VBA,我想知道如何添加过程来计算每个序列的长度。 Sub Collatz() Dim i
我正在尝试减去 CSV 中的两列以创建第三列“持续时间”结束时间 - 开始时间 每一行也对应一个用户 ID。 我可以创建一个仅包含“持续时间”列的 csv 文件,但我宁愿将其重定向回原始 csv。 例
我在 2018.04 玩这个最长的 token 匹配,但我认为最长的 token 不匹配: say 'aaaaaaaaa' ~~ m/ | a+? | a+ /; # 「a」
因此,按照规范规定最终用户/应用程序提供的给定变量(200 字节)的字节长度。 使用 python 字符串,字符串的最大字符长度是多少,满足 200 字节,因此我可以指定我的数据库字段的 max_le
我需要针对我们的Jenkins构建集群生成每周报告。报告之一是显示具有最长构建时间的作业列表。 我能想到的解决方案是解析每个从属服务器(也是主服务器)上的“构建历史”页面,对于作业的每个构建,都解析该
我正在构建一个 iOS 应用程序,它将流式传输最长为 15 秒的视频。我阅读了有关 HLS 的好文章,因此我一直在对片段大小为 5 秒的视频进行转码。如果视频的第一部分加载时间太长,那么我们可以在接下
docs for Perl 6 longest alternation in regexes punt to Synopsis 5记录 longest token matching 的规则.如果不同的
我是一名优秀的程序员,十分优秀!