I'm building a search function on clientside, where i'm matching a search for ingredients within an array.

If the user types more than one word (divided by space), i'd like to find a match for all those words. If not found after that, i'll match any of those words.

So the second regexp looks like this if the search input is "strawberry pie":

/^strawberry|^pie|\sstrawberry|\spie/gi

The first one is more complex:

/
^strawberry.*\spie|\sstrawberry.*\spie|
^pie.*\sstrawberry|\spie.*\sstrawberry
/gi

...to match "pie, strawberry" as well as "strawberry pie" and "creamy strawberry pie", or whatever...


If someone has a better solution to accomplish the same thing would be great. For example if there is an "and" operator I could use in the same mannor as "or" | .

Else I need to mount a complex RegExp. To do that I'd like some hints/help with an algorithm that go through an array as follows:

Code:
str="";
// if the array has two items:
searchInput=["| x |","| y |"];
str+="two items:<br>";
for (var i=0;i<searchInput.length;i++) {
    for (var j=0;j<searchInput.length;j++) {
        if (i!=j) {
            str+=searchInput[i]+" "+searchInput[j];
            str+="<br>";
        }
    }
}

// three items:
searchInput=["| x |","| y |","| z |"];
str+="three items:<br>";
for (var i=0;i<searchInput.length;i++) {
    for (var j=0;j<searchInput.length;j++) {
        for (var k=0;k<searchInput.length;k++) {
            if (i!=j&&i!=k&&j!=k) {
                str+=searchInput[i]+" "+searchInput[j]+" "+searchInput[k];
                str+="<br>";
            }
        }
    }
}

// four items;
searchInput=["| x |","| y |","| z |","| o |"];
str+="four items:<br>";
for (var i=0;i<searchInput.length;i++) {
    for (var j=0;j<searchInput.length;j++) {
        for (var k=0;k<searchInput.length;k++) {
            for (var m=0;m<searchInput.length;m++) {
                if (i!=j&&i!=k&&j!=k&&i!=m&&j!=m&&k!=m) {
                    str+=searchInput[i]+" "+searchInput[j]+" "+searchInput[k]+" "+searchInput[m];
                    str+="<br>";
                }
            }
        }
    }
}

document.write(str);
In other words, I'd like to go through all combinations of an array.

Help is much appreciated!