2

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?

2
  • Your RegExp reads as "ipad dn(s|d)ns system", you're missing parenthesis. Commented Aug 31, 2015 at 21:22
  • Using if (data[i][0].match(keywords)) {, I obtained 5 results: dns system, dns systems, ipad dns, ipad dns system, ipad dns register - is it what you are looking for? You forgot to call .match(). Or test(): if (keywords.test(data[i][0])) { Commented Aug 31, 2015 at 21:23

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.