Reputation: 5924
I'm using a Google Script to extract values from a column that match my regex, but I can't seem to get my regex variable to work with my if statement. When I set the variable to a string, I have no problem with running my function and getting a response back, but it isn't working with the regex and I'm trying to figure out the issue:
Here is my script:
function extractBranded() {
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
for (var i = 0; i < data.length; i++) {
var keywords = /ipad dns|dns system/;
if (data[i][0] == keywords) {
Logger.log(data[i][0]);
} else {
}
}
Google Sheets Data (Queries = Column and Row A1):
Queries
dns system
dns systems
ipad dns
ipad dns system
business 101
ipad dns register
dns
In theory this should pick up on four of the seven values in my sheet.
I also tried calling a constructor function with new RegExp()
, but this did not work. Any thoughts?
Upvotes: 2
Views: 2078
Reputation: 627469
Using
if (data[i][0].match(keywords)) {
Or
if (keywords.test(data[i][0])) {
You will obtain 5 results:
[15-08-31 23:27:52:850 CEST] dns system
[15-08-31 23:27:52:864 CEST] dns systems
[15-08-31 23:27:52:865 CEST] ipad dns
[15-08-31 23:27:52:866 CEST] ipad dns system
[15-08-31 23:27:52:866 CEST] ipad dns register
You forgot to call match()
or test()
to actually apply your regex.
Upvotes: 4