javascript - search multiple strings in a sentence

Viewed 68

Using javascript, I'm running through an array of sentences & searching multiple patterns in each of those(Case insensitive search).

The array contents is as below ( output from firebug)-

var names=[
    "SUBSET FULL ACT # - TOTAL",
    "SUBSET  ACT ACTIV - TOTAL",
    "SUBSET ACT ACTIV - AVERAGE 1th LOAD",
    "SUBSET ACT ACTIV - AVERAGE",
    "SUBSET ACT ACTIV - AVERAGE 2th LOAD",
    "SUBSET ACT ACTIV - AVERAGE 3th LOAD",
    "SUBSET ACT ACTIV - AVERAGE4th LOAD",
    "SUBSET ACT ACTIV - MAX",
];

Here's what I've tried so far.

for (j = 0; j < names.length; ++j) {
    var searchRes = names[j].replace(/\s/g, "").toLowerCase().search(/'average'|'load'/);
    if (searchRes === -1) {
        doThis = "facr1";
    } else {
        doThis = "facr2";
    }

The search result is always -1 though the pattern exists. I've looked up on the internet & find this is one of the common ways to search strings in javascript.

Could I please request a help from a second pair of eyes to help me spot what I'm doing wrong here?

2 Answers
Related