cypress.should contains with regex with variable and substring

3.6k Views Asked by At

How can I use regular expression with variable and substring in cypress.should to verify url by typescript? something like?

const string = /key_to_be_include\w+key1__`${v1}`,key2__`${v2}`/
//url: example.com/subdomain1/subdomainb/?key_xxxx=xxxxx&key_to_be_include=~alltextneed%25needtoBypass,key1__value1,key2__value2&...

const v1 = 'value1'
const v2 = 'value2'
const string = /key_to_be_include\w+key1__`${v1}`,key2__`${v2}`/
cy.url().should('contains', string);
1

There are 1 best solutions below

0
Jboucly On

You can use contains() !

const string = /key_to_be_include\w+key1__`${v1}`,key2__`${v2}`/
cy.url().contains(string);

See here

Or try this or something like that :

const string = /key_to_be_include\w+key1__`${v1}`,key2__`${v2}`/
cy.url().should((url) => {
   expect(url).to.match(string)
}

See here