1

I need some simple javascript code for a Vue application that will take a string split it into an array and check if any value is in a different string.

I have

              let AffiliationString = " This person goes to Stony Brook"

              let affiliation = "Stony Brook OR Stony Brook University OR The University of Washington"
              let affiliations = affiliation.toLowerCase().split(" or ");
              affiliation = affiliations.join(",");
               let regexList = [ affiliation ];

               let isMatch = regexList.some(rx => rx.test(AffiliationString));

I want to see if any item in the array is in the "AffiliationString" string

When I do this I get the following error

       Uncaught (in promise) TypeError: rx.test is not a function

I have seen many samples on StackOverflow of checking the array if a value is there but not the other way around. I'm trying to use javascript - match regular expression against the array of items

I'm doing this in a Vue project with

         "eslint": "6.7.2",

do I need to redo it as a loop for each value in the array?

Thanks for your help

Bill
  • 1,423
  • 2
  • 27
  • 51

1 Answers1

1

You're not actually making RegExp's out of your affiliation string, which is why rx.test is not a function. You can make a RegExp which will match against all the affiliation string pieces at once, by separating them with |. We wrap each element in the regex in \b so that (for example) Brook doesn't match Brooktown. Adding the i flag to the RegExp makes it case-insensitive:

let AffiliationString = " This person goes to Stony Brook"
let affiliation = "Stony Brook OR Stony Brook University OR The University of Washington"
let regex = new RegExp('\\b' + affiliation.split(/\s+or\s+/i).join('\\b|\\b') + '\\b', 'i')
let isMatch = regex.test(AffiliationString)
console.log(isMatch)
Nick
  • 138,499
  • 22
  • 57
  • 95