This is not as neat as @Nirk's solution (I always forget about Array.filter) but you can replace my test function with Array.filter per his answer:
function test(a, f) {
var i = 0,
result = [];
for (i=0; i<a.length; i++) {
result.push(f(a[i]));
}
return result;
}
function txtMatch(s) {
var pattern = new RegExp('' + s + '');
return function(t) {
return pattern.test(t);
};
}
var sets= new Array()
sets[0]='nnd';
sets[1]='nndha';
sets[2]='ch';
sets[3]='gn';
var toMatchFirst = test(sets, txtMatch('nn'));
var toMatchSecond = test(sets, txtMatch('c'));
console.log(toMatchFirst.join(', '));
console.log(toMatchSecond.join(', '));
I just created a quick string match function and then loop through the array, testing each, and returning an array of the results.