luishengjie
luishengjie

Reputation: 187

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.

Upvotes: 0

Views: 91

Answers (1)

user663031
user663031

Reputation:

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);

Your problem is a subset of a general problem sometimes called "fuzzy (or approximate) string matching", where you are matching in the presence of one or more replacements (your case), inserted or removed characters, or transposed characters. This general problem could certainly not be solved with regexp. But you'll find plenty of libraries to do this. You could start off by looking at https://code.google.com/archive/p/google-diff-match-patch/.

Upvotes: 1

Related Questions