diff --git a/packages/playwright-core/src/client/locator.ts b/packages/playwright-core/src/client/locator.ts index f06897a31e..2e4c37081a 100644 --- a/packages/playwright-core/src/client/locator.ts +++ b/packages/playwright-core/src/client/locator.ts @@ -99,9 +99,9 @@ export class Locator implements api.Locator { } withText(text: string | RegExp): Locator { - const matcher = isRegExp(text) ? 'text-matches' : 'has-text'; - const source = escapeWithQuotes(isRegExp(text) ? text.source : text, '"'); - return new Locator(this._frame, this._selector + ` >> :scope:${matcher}(${source})`); + if (isRegExp(text)) + return new Locator(this._frame, this._selector + ` >> :scope:text-matches(${escapeWithQuotes(text.source, '"')}, "${text.flags}")`); + return new Locator(this._frame, this._selector + ` >> :scope:has-text(${escapeWithQuotes(text, '"')})`); } frameLocator(selector: string): FrameLocator { diff --git a/tests/page/locator-query.spec.ts b/tests/page/locator-query.spec.ts index fb775fc27e..51de7bb365 100644 --- a/tests/page/locator-query.spec.ts +++ b/tests/page/locator-query.spec.ts @@ -84,3 +84,8 @@ it('should filter by regex with quotes', async ({ page }) => { await page.setContent(`