3

I need a regex pattern to check whether characters present in a string consecutively repeats 3 character in another string. Eg:

var string1 = "HelloWorld";    
var string2 = "Work";

Here the letters "Wor" in string1 repeats in string2, so it should return true.

Any help on this

Salman A
  • 262,204
  • 82
  • 430
  • 521
Paka
  • 1,053
  • 11
  • 20

4 Answers4

3

Code from 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));
}

Some test cases:

findOverlap("12345", "aaa") // ""
findOverlap("12345", "12") // "12"
findOverlap("12345", "345") // "345"
findOverlap("12345", "3456") // "345"
findOverlap("12345", "111") // "1"

To solve your particular problem you could:

const haveOverlap = (string1, string2) => findOverlap(string1, string2).length >= 3;
console.log(haveOverlap('HelloWorld', 'Work')); // true
Jonny
  • 2,223
  • 23
  • 30
  • It should have been `length >= 3` instead of `length > 3` of course. Fixed my code example. – Jonny Dec 07 '17 at 12:20
3

Another idea would be to concatenate the two strings, lowercase them and then apply this regular expression:

(\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"));

Here is a more robust version which prevents finding the string repetition in either of the input strings:

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"));
gil.fernandes
  • 12,978
  • 5
  • 63
  • 76
2

Use split, substring and 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));
Paka
  • 1,053
  • 11
  • 20
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
0

Slightly longer than the other answers here, and nothing too clever about it. Will simply go through and find all matches, which returns an array, and will loop through that array and return true when the longer string contains one of the split strings.

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))
KyleFairns
  • 2,947
  • 1
  • 15
  • 35