Rayrun
← Back to Discord Forum

expect().toMatch() with regexp

Hi, I'm trying to apply the regex to check expect... All on-line validator says, my regex should match... however, when running the code, I'm getting an error with expect... Could you advise what is wrong with that? Here is an example: Expected substring: "/cloudformation_[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}_customer_cspm-mercury.yaml/" Received string: "cloudformation_31791442-9122-4ce1-8b88-7c243d02d94f_customer_cspm-mercury.yaml" btw. I also tried without '/'.... This string contains UUID in the middle. 😉

This thread is trying to answer question "Why is the regex not working with expect().toMatch() despite working in online validators?"

5 replies

Your regexp works in a regular expression tester, so would help to see a code snippet for implementation. You can create a new RegExp or use the string value.

Some examples that work for me:

const pageTitleMatch = /.*Products/i const userUrlMatch = new RegExp(/.*${process.env.BASE_URL}/user, 'i');

I am ignoring case since we have some old to new UI issues on the case sensitivity, but the above are examples of how I test titles and url routes.

In the .toMatch() you need to ensure that you are passing in a RegExp over a string.

expect(value).toMatch(/cloudformation_[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}_customer_cspm-mercury.yaml/)

should work, so would need to see more of your code.

BTW, I tested your regexp on https://regex101.com

Hey! Thanks a lot for the example... it seems I used the method in a wrong way... My bad! 🙂

It seems my eyes were ignoring the single quotes... And that was too much. Thanks again!

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].