Unsure how to replace hardcoded regex with one from a variable in my Cypress test

I have the following code

checkLocationsInTable(location) {
        cy.datacy('locationCell').each(($el) => {
            cy.wrap($el).invoke('text').should('match', /Cheltenham \(Remote\)|Home/i)
        }).then(($els) => {
            cy.datacy('searchResults').should('contain', $els.length.toString())
        })
    }

Which works as I want, however I would like to remove the hardcoded regex with one created from the value contained in the location variable.

The location variable will consist of an array of text, long the lines of this

['Cheltenham (Remote)', 'Home'])

I tried this

checkLocationsInTable(location) {
        let output = location.join('|').replaceAll("(", "/(").replaceAll(")", "/)")

        cy.datacy('locationCell').each(($el) => {
            cy.wrap($el).invoke('text').should('match', new RegExp(output), 'g')
        }).then(($els) => {
            cy.datacy('searchResults').should('contain', $els.length.toString())
        })
    }

But it doesn’t work.

  1. I don’t think the β€˜g’ is needed.
  2. I’d have a look into Regular expressions - JavaScript | MDN .Hint: the escape character you use for dealing with Regex special chars might not be right one.

Removing the g didn’t help.

What about sth like this?

      cy.datacy('locationCell').each(($el) => {
        cy.wrap($el)
          .invoke('text')
          .then((text) => {
            expect(text.trim()).to.match(/Cheltenham \(Remote\)|Home/i);
          });