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

1 Answer 1

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)

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.