I am having problems matching the following URLs with Cypress intercept() using regex. What am I doing wrong?
cy.intercept('GET', /https:\/\/mysite\.me\.com\/pages\/[^\/] [0-9](?=\/$|$)/, { fixture: 'pages.json' }).as('getPage');
Matching URLs:
Non-matching URLs:
- https://mysite.me.com/pages/?something=something
- https://mysite.me.com/pages/1234/something
- https://mysite.me.com/pages/something
I would ultimately like to use the regex with an env variable but first things first.
cy.intercept('GET', `${Cypress.env('API_PATH')}/pages/[^\/] [0-9](?=\/$|$)/`, { fixture: 'pages.json' }).as('getPages');
CodePudding user response:
That regex looks unnecessarily complicated, try to simplify it to something like:
/\/pages\/[0-9] [/]?$/
that will match both:
/pages/1234
and:
/pages/1234/
[^\/]
You don't need \
because /
has a different meaning inside a character class.
(?=\/$|$)
Ok, so the /
at the end is optional. This is more readable: [/]?
, or even \/?
.
And I don't think you need to be as explicit as mentioning the base url in the pattern.