I just took advantage of this feature. It returns true if text contains a substring value ; even if the letters are scrambled **:
function fuzzyContains(text, value) { // for value = "THE", build the regex /[the]{3}/ and test var valueCopy = value.toLowerCase().replace(/\W/g, ""); var regexp = new RegExp("[" + valueCopy + "]{" + valueCopy.length + "}"); return regexp.test(text.toLowerCase()); } fuzzyContains("Netherlands", "Nethe") // true fuzzyContains("Netherlands", "Neteh") // true fuzzyContains("Netherlands", "Nethl") // false
Demo here
Update
Here is the revised version of the function. It returns true if text contains all characters from value **:
function fuzzyContains_v2(text, value) { var textCopy = text.toLowerCase(); var valueCopy = value.toLowerCase().split(""); console.log(textCopy, valueCopy); var a = $.grep(valueCopy, function (value) { return textCopy.indexOf(value) == -1; }); return a.length == 0; } fuzzyContains("Netherlands", "Nethe") // true fuzzyContains("Netherlands", "Neteh") // true fuzzyContains("Netherlands", "Nethl") // true fuzzyContains("Netherlands", "Netlands") // true
Demo here
** I have not tested the behavior when value contains duplicate characters. However, I am sure that the results will still be acceptable.
source share