javascript - Is it possible to get similar text in string by RegExp -
is possible make function similar text in string regexp ex:
max differences letters = 2 or 3 letters ( + or - ) fine
var text = 'is possible similar text in string regexp'; // , similar_text(text , 'strong'); // => string similar_text(text , 'posible'); // => possible similar_text(text , 'isit'); // => // etc...
here implementation (needs levenshtein function). not prettiest, gets job done:
function similar_text(haystack, needle) { var best_match = false; var best_match_int = 9007199254740992; var arr = haystack.split(" "); ( var chunk = 1; chunk < arr.length; chunk++ ) { ( var = 0; i<arr.length; i++ ) { if ( i+chunk > arr.length ) continue; var substack = arr.slice(i, i+chunk).join(' '); var l = new levenshtein(needle.tolowercase(), substack.tolowercase()); if ( l.distance < best_match_int ) { best_match = substack; best_match_int = l.distance; } } } return best_match; }
here plunker demoing how can done.
Comments
Post a Comment