I'm writing a search tool program in javascript that has a simple keyword search. This piece of code basically checks to see that the keyword field isn't empty, then it uses a regex to check for any matches against an array. If there are matches, it sets a variable to be true and later uses to check again before pushing onto another array.
So the keyword search works for most of my keywords except where I have a plus sign involved. One of the keywords is 3+2
, it matches when I test it with 3+
or 2
or 3
, but it doesn't match when I test it with +
or +2
or 3+2
.
On top of that, if I start it with a +
or +2
, it doesn't execute this piece of code. It's like there's nothing that was entered.
I would appreciate any insight.
if (this.keywords_val !== ''){
var regex = new RegExp(this.keywords_val, "gi");
if (degrees[i].keywords.replace(/ /g, '').match(regex)) check_keyword = true;
else check_keyword = false;
}
if ((check === true) && (check_keyword === true)) matches.push(degrees[i]);
else nonmatches.push(degrees[i].name.replace(/ /g, ''));