我需要一个正则表达式模式来检查字符串中的字符是否连续重复另一个字符串中的3个字符。例如:
var string1 = "HelloWorld";
var string2 = "Work";
这里string1中的字母“Wor”在string2中重复,因此它应该返回true。
对此有任何帮助
答案 0 :(得分:3)
来自https://www.garysieling.com/blog/javascript-function-find-overlap-two-strings的代码:
function findOverlap(a, b) {
if (b.length === 0) {
return '';
}
if (a.endsWith(b)) {
return b;
}
if (a.indexOf(b) >= 0) {
return b;
}
return findOverlap(a, b.substring(0, b.length - 1));
}
一些测试用例:
findOverlap("12345", "aaa") // ""
findOverlap("12345", "12") // "12"
findOverlap("12345", "345") // "345"
findOverlap("12345", "3456") // "345"
findOverlap("12345", "111") // "1"
要解决您的特定问题,您可以:
const haveOverlap = (string1, string2) => findOverlap(string1, string2).length >= 3;
console.log(haveOverlap('HelloWorld', 'Work')); // true
答案 1 :(得分:3)
另一个想法是连接两个字符串,小写它们然后应用这个正则表达式:
(\w{3}).+(\1)
function repetitions(s1, s2) {
const s = s1.toLowerCase() + s2.toLowerCase()
const r = /(\w{3}).+(\1)/
const res = r.exec(s)
return res !== null ? res[1] : "";
}
console.log(repetitions("HelloWorld", "Work"));
console.log(repetitions("HelloWo", "Work"));
这是一个更强大的版本,可以防止在任何一个输入字符串中找到字符串重复:
function repetitions(s1, s2) {
const replaceRegex = /(\w{3})(.*)(\1)/;
const s = s1.toLowerCase().replace(replaceRegex, "$1") + " " + s2.toLowerCase().replace(replaceRegex, "$1");
const r = /(\w{3}).+?(\1)/;
const res = r.exec(s);
return res !== null ? res[1] : "";
}
console.log(repetitions("HelloWorld", "Work"));
console.log(repetitions("HelloWo", "Work"));
console.log(repetitions("HelloWoHello", "Work"));
console.log(repetitions("HelloWoHello", "WorkWork"));
console.log(repetitions("HelloWo", "HelloWork"));
答案 2 :(得分:2)
使用split
,substring
和includes
var fn = function( string1, string2, matchChars ) {
return !!string2.split("").find( function(item, index){
if (index + matchChars <= string2.length )
{
return string1.includes( string2.substring( index, index + matchChars ) ); //check after each turn if the substring from index is included in string1 or not
}
return false;
});
}
console.log( fn("HelloWorld", "Work", 3) );
var fn = function(string1, string2, matchChars) {
return !!string2.split("").find(function(item, index) {
if (index + matchChars <= string2.length) {
return string1.includes(string2.substring(index, matchChars));
}
return false;
});
}
console.log(fn("HelloWorld", "Work", 3));
console.log(fn("HelloWorld", "Wod", 3));
&#13;
答案 3 :(得分:0)
比这里的其他答案略长,而且没什么太聪明的。将直接查找所有匹配,返回一个数组,并循环遍历该数组,并在较长的字符串包含其中一个拆分字符串时返回true。
function getAllConsecutives(string, number) {
let matches = [],
regex = "";
for (let i = 0; i < (string.length - number); i++) {
regex = new RegExp(`\\w{${i}}(\\w{${number}})`);
matches.push(string.match(regex)[1])
}
return matches
}
function areThereConsecutives(string1, string2, number) {
let short = string1.length < string2.length ? string1 : string2,
long = string1.length < string2.length ? string2 : string1,
consecutives = getAllConsecutives(short, number);
for (let i = 0; i < consecutives.length; i++) {
if (long.includes(consecutives[i])) {
return true;
}
}
return false
}
let string1 = "HelloWorld",
string2 = "Work";
console.log(areThereConsecutives(string1, string2, 3))
答案 4 :(得分:0)
你的问题是finding longest common substring的问题。这是该算法的JavaScript实现:
function longestCommonSubstring(string1, string2) {
var length1 = string1.length,
length2 = string2.length,
matchArray = Array(length1).fill().map(function() { return Array(length2).fill(); }),
matchSize = 0,
matchList = [],
i, j;
for (i = 0; i < length1; i++) {
for (j = 0; j < length2; j++) {
if (string1[i] === string2[j]) {
if (i === 0 || j === 0) {
matchArray[i][j] = 1;
} else {
matchArray[i][j] = matchArray[i - 1][j - 1] + 1;
}
if (matchArray[i][j] >= matchSize) {
if (matchArray[i][j] > matchSize) {
matchSize = matchArray[i][j];
matchList = [];
}
matchList.push(string1.substring(i - matchSize + 1, i + 1));
}
} else {
matchArray[i][j] = 0;
}
}
}
return matchList;
}
console.log(longestCommonSubstring("abcdefghi", "def"));
console.log(longestCommonSubstring("abcdefghi", "*def*"));
console.log(longestCommonSubstring("ababababa", "ab"));
console.log(longestCommonSubstring("ababababa", "ba"));
console.log(longestCommonSubstring("ababababa", "aba"));
console.log(longestCommonSubstring("HelloWorld", "Work"));
&#13;
此处提供的其他实施方案:
Algorithm Implementation/Strings/Longest common substring