Alex Daro
Alex Daro

Reputation: 429

Javascript - how to search by multiple strings

Let's say I have a search term like so: "ice cream"

and let's say I have three strings like so:

["Jeni's Ice Cream","Ice Cream Labs","Zumbacream"]

How could I match using regex the first two items in the list but not the third?

Upvotes: 0

Views: 1030

Answers (4)

squill25
squill25

Reputation: 1208

To add onto Yang Li's answer, I would split the search term by a space so that you can check if the strings contain the search term, but the order of the words wouldn't matter, i.e. it matches the string if it contains every word of the search term, but not necessarily in the same order:

var items = ["Jeni's Ice Cream","Ice Cream Labs","Zumbacream"]

var searchTerm = 'Ice Cream';
searchTerm = searchTerm.toLowerCase();
var searchTerms = searchTerms.split(' ');

for (var i = 0; i < items.length; i++){
    for (var i2 = 0; i < searchTerms.length; i2++){
        if (items[i].toLowerCase().indexOf(searchTerms[i2]) == -1) { // if the string we're checking does not contain a word in the search term
            break;
        } else if (i2 < searchTerms.length - 1) { // or if it's not the last word of the search term
            continue;
        }
        // if it's the last search term and the string contains all of the search words
        alert(items[i] + " - matched");
    }
}

Fiddle: http://jsfiddle.net/rd8ujg1t/2/

Upvotes: 0

PaulK
PaulK

Reputation: 16

For a regex version:

var items = ["Jeni's Ice Cream","Ice Cream Labs","Zumbacream"];
var term = /Ice Cream/i;//take the i out for a case sensitive search

var matched = items.filter(function(comp){return comp.match(term) != null;});

Upvotes: 0

user3117575
user3117575

Reputation:

Although this would be inefficient on a large scale (which is one of the reasons why database engines were made), you could index through each item and see if it matches the searchTerm

var searchTerm = "ice cream",
searchTermRgx = new RegExp(searchTerm, "i");
database = [
    "Jamen's Ice Cream Shop", 
    "Dat good ice cream", 
    "Pizza Place",
    "More Ice Cream"
], i,
searchResults = [];

for (i=0; i<database.length; i++) {
  if ((searchTermRgx).test(database[i])) searchResults.push(database[i]);
}

document.body.innerHTML = searchResults;

(Note that using the RegEx i operator, it's matching case-insensitively)

JSFiddle Example

Upvotes: 0

yangli-io
yangli-io

Reputation: 17334

var items = ["Jeni's Ice Cream","Ice Cream Labs","Zumbacream"]

var searchTerm = 'Ice Cream';

for (var i = 0; i < items.length; i++){
    if (items[i].indexOf(searchTerm) > -1){
        console.log(items[i] + " - matched");
    } else {
        console.log(items[i] + " - unmatched");
    }
}

For non-sensitive casing

var items = ["Jeni's Ice Cream","Ice Cream Labs","Zumbacream"]

var searchTerm = 'ice cream';

searchTerm = searchTerm.toLowerCase();

for (var i = 0; i < items.length; i++){
    if (items[i].toLowerCase().indexOf(searchTerm) > -1){
        console.log(items[i] + " - matched");
    } else {
        console.log(items[i] + " - unmatched");
    }
}

Alternatively you could create a prototype to Array and use the filter function

Array.prototype.search = function(searchTerm, caseSensitive){
    if (!caseSensitive){
        searchTerm = searchTerm.toLowerCase();
    }
    function search(value){
        if (!caseSensitive){
            value = value.toLowerCase();
        }
        return value.indexOf(searchTerm) > -1;
    }

    return this.filter(search);
}

To use this

items.search('ice cream', false)

This will return

["Jeni's Ice Cream", "Ice Cream Labs"]

Upvotes: 3

Related Questions