cy.contains match with regex?

This solution can also be useful:

cy.get('div')                // select DOM element (tag, class or id)
  .invoke('text')            // check the innerHTML text
  .should('match', /regex/)  // compare with a regular expression

More on: https://docs.cypress.io/api/commands/invoke


If you are trying to see if some content on your website has the text http://www.mywebsite.com/get-stuff using regex, you will need to pass in a valid Regular Expression. Your argument is attempting to match using a glob expression.

If you are trying to see if the url of your website is navigated to http://www.mywebsite.com/get-stuff, you likely want to write an assertion off of the cy.url() command like so:

cy.url().should('match', /myregexp/)

I know it's been quite a long, but for those who are still looking for a solution ( just like me ), you can make use of cy.url().should('contain', /regex/) if the accepted solution didn't work for you.

Tags:

Cypress