1

I can't get IndexOf to match correctly in GAS

I've tested out my regular expression here: https://regex101.com/r/0cK6xQ/1

As soon as there is a space inside the string, indexOf() will not match. I even tried setting the regex as let contactRegExp = /(contact 1 Type)/i ; , which should be a direct match for the 2nd element in the sample array and it bombs out.

function setContactTypes(){  

 //Find all contact Type columns
 let contactRegExp = /(contact [\d]* Type)/i ;

 var headerIndexList = ['dummy1','contact 1 Type','dummy2','contact 2 Type'];

 var hitArray = [];
 var i = -1;
 while ((i = headerIndexList.indexOf(contactRegExp,(i+1))) != -1){
     hitArray.push(i);
  }
}

hitArray should return [1,3]

I'm thinking it has something to do with an Array vs a string, but for the life of me can't figure it out.

1
  • You could use String.prototype.search() to search for patern into the strings of your array Commented Oct 21, 2021 at 7:57

1 Answer 1

2

Unfortunately, Array.prototype.indexOf() cannot use the regex. So in your situation, in order to achieve your goal, how about the following modified script?

Modified script:

function setContactTypes(){  
 let contactRegExp = /(contact [\d]* Type)/i ;
 var headerIndexList = ['dummy1','contact 1 Type','dummy2','contact 2 Type'];
 var hitArray = headerIndexList.reduce((ar, e, i) => {
   if (contactRegExp.test(e)) ar.push(i);
   return ar;
  }, []);
  console.log(hitArray) // When your "headerIndexList" is used, [1, 3] is returned.
}

Testing:

let contactRegExp = /(contact [\d]* Type)/i ;
var headerIndexList = ['dummy1','contact 1 Type','dummy2','contact 2 Type'];
var hitArray = headerIndexList.reduce((ar, e, i) => {
  if (contactRegExp.test(e)) ar.push(i);
  return ar;
}, []);
console.log(hitArray)

References:

Added:

For example, the following script is useful for your situation?

let contactRegExp = /(contact [\d]* Type)/i;
var headerIndexList = ['dummy1', 'contact 1 Type', 'dummy2', 'contact 2 Type'];
var hitArray = [];
for (var i = 0; i < headerIndexList.length; i++) {
  if (contactRegExp.test(headerIndexList[i])) {
    hitArray.push(i);
  }
}
console.log(hitArray)

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

5 Comments

Thank you @Tanaike. This is close. Would prefer to use the i value as input for another set of instructions right away. However I can use the output array and loop through it with a few extra steps. Still racking my brain on how the reduce function works.
@ThePirate Thank you for replying. I have to apologize for my poor English skill. I cannot understand Would prefer to use the i value as input for another set of instructions right away.. I thought that you wanted to retrieve [1, 3] from headerIndexList and contactRegExp. Is my understanding correct? Can I ask you about the detail of your goal?
大丈夫です. Your English is better than my Japanese. while ((i = headerIndexList.indexOf(contactRegExp,(i+1))) != -1){ // callotherFunction(i); // callmoreFunction(i, 'string'); } You have been a big help.. Thank you.
@ThePirate Thank you for replying. Now I added one more sample script using the for loop. Is that useful for your situation? If that was not useful, I apologize again.
いい頭. Perfect. ありがとうございました. Your added script works perfect.

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.