Find the word with letters in the wrong sequence using JS / jQuery

How can I find a word that does not match the searchcriteria query but has several identical letters in it?

Here's a fiddle to play around a bit.

If the user enters au , the first 2 entries are displayed. But how can I achieve that the same thing happens when a user types in ua ?

Js

I use the following to find the value from input and check if it exists in the list:

 $("#search").on("keyup click", function () { var value = this.value.toLowerCase(); $("#list").children().each(function () { var text = this.innerText.toLowerCase(); $(this)[text.indexOf(value) !== 0 ? 'hide' : 'show'](); }); }); 

HTML

 <input type="search" id="search"> <ul id="list"> <li><a href="#/australia/">Australia</a></li> <li><a href="#/austria/">Austria</a></li> <li><a href="#/belgium/">Belgium</a></li> <li><a href="#/brazil/">Brazil</a></li> <li><a href="#/canada/">Canada</a></li> <li><a href="#/denmark/">Denmark</a></li> <li><a href="#/finland/">Finland</a></li> <li><a href="#/france/">France</a></li> <li><a href="#/germany/">Germany</a></li> ...and so on... </ul> 

I am not very familiar with RegExp , but I think this will do the trick ??

+4
source share
3 answers

This is not possible with RegExp, however you can use the following function to meet your needs:

 function looseCharacterMatch(a, b) { a = a.split(""); b = b.substring(0,a.length); var c = true; for (var i = 0; i < a.length; i++) { if (b.replace(a[i], "") == b) { c = false; } b = b.replace(a[i], ""); } return c; } 

Demo | Demo source

+2
source

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.

+3
source

Try the following:

 var text = this.innerText.toLowerCase(); text = text.replace(/([az]*)([AZ]*)/, '$2$1'); 
0
source

All Articles