entirely in JS, no server backend. I need to allow the user to search and then show a list of matched names. I'm using jQuery UI AutoComplete but need some JS to filter the results.
Given an Array of names:
Bob Hope
James Jones
Steve Jobs
Larry McBridge
Given a search term like Bo
How can I get just Bob Hope to return
Given a Search term like b:
How can I get all but James Jones?
Any simple JS for comparing two strings? Thanks
A fuzzy search searches for text that matches a term closely instead of exactly. Fuzzy searches help you find relevant results even when the search terms are misspelled. To perform a fuzzy search, append a tilde (~) at the end of the search term.
Fuzzy searching matches the meaning, not necessarily the precise wording or specified phrases. It performs something the same as full-text search against data to see likely misspellings and approximate string matching.
Fuzzy string matching can help improve data quality and accuracy by data deduplication, identification of false-positives etc.
var names = ["Bob Hope","James Jones","Steve Jobs","Larry McBridge"]
var query = "bo"
var results = $(names)
.map(function(i,v){
if(v.toLowerCase().indexOf(query.toLowerCase())!=-1){return v}
}).get()
// results is ["Bob Hope"]
Maybe I misunderstood you (given the complexity of the above answer), but I came up with this which uses jQuery. Every time a key is pressed (when the input has focus), it searches all li
elements on the page and finds whether they contain the search term.
Here is the HTML:
<ul><li>Bob Hope</li>
<li>James Jones</li>
<li>Steve Jobs</li>
<li>Larry McBridge</li></ul><br>
Search: <input id="search" length="24" />
And here is your jQuery:
$("#search").keyup(function(){
$("li").hide();
var term = $(this).val();
$("li:contains('" + term + "')").show();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With