regex - Regular expression to match part of a word



Is it possible to match a pattern of a word with a certain margin of error. For example a words "Tike", "Make", "Bake", "Tame" fits the pattern of the word take "Take" with one misspelled character.

1 Answer: 

Regexp is not a very scalable solution to this problem, but if this is really all you want to do, then use a regexp with multiple alternatives (separated by |), each with one character replaced by the dot (.) wildcard.

function makeMatcher(str) {
  const regexp = [...str].map((chr, i) => 
    `${str.slice(0, i)}.${str.slice(i+1)}`).join('|');
  console.log("regexp is", regexp);
  return new RegExp(regexp, "i");

function test(s) { console.log(s, matcher.test(s) ? "matches" : "does not match"); }

const matcher = makeMatcher("Mike");

["Mike", "Mila", "Mika", "John"].forEach(test);