是否可以通过RegExp ex:
创建函数来获取字符串中的类似文本最大差异字母= 2或3个字母(+或 - )将是正常的
var text = 'Is it possible to get similar text in string by RegExp';
// and
similar_text(text , 'strong'); // => string
similar_text(text , 'posible'); // => possible
similar_text(text , 'isit'); // => Is it
// etc...
答案 0 :(得分:7)
这是我的实现(需要adding items功能)。不是最漂亮的,但它完成了工作:
function similar_text(haystack, needle) {
let best_match = false;
let best_match_int = 9007199254740992;
let arr = haystack.split(" ");
haystack = haystack.toLowerCase();
needle = needle.toLowerCase();
for (let chunk = 1; chunk < arr.length; chunk++) {
for (let i = 0; i < arr.length; i++) {
if (i + chunk > arr.length)
continue;
let subStack = arr.slice(i, i + chunk).join(' ');
let l = new Levenshtein(needle, subStack);
if (l.distance < best_match_int) {
best_match = subStack;
best_match_int = l.distance;
}
}
}
return best_match;
}
以下是Levenshtein演示如何完成此操作。