Search code examples
playwright

How expect toMatch works?


I'm doing a POC with playwright with {expect} from '@playwright/test'.

I'm a bit confused how to create the regexp, I expect to validate this string which is correct as per the regex validator.
expect('abc123').toMatch('abc(\d+)')

first the '\' is marked as Unnecessary escape character
tried '\\' marks error
removed '\' marks error

enter image description here


Solution

  • How about toMatchText:

    import { expect } from '@playwright/test';
    
    await expect('abc123').toMatchText(/abc\d+/);
    

    I don't see toMatch in the docs, so I used toMatchText(). I also think that capturing group in the regex () is not necessary in this example, so \d+ should be enough.