A Shelf test checks that the fees page shows a total of 12. The page has a bug and shows 120. The test passes.
const pattern = /Total: 12/;
console.log(pattern.test("Total: 12"));
console.log(pattern.test("Total: 120"));
console.log(pattern.test("Grand Total: 12"));
true
true
true
A regular expression is a pattern for text. You write it between two slashes. test answers whether the pattern is found in a string.
"Found in" is the important part. The pattern /Total: 12/ matches any text that contains those characters anywhere. "Total: 120" contains them. So does "Grand Total: 12". Capital letters count, so "Subtotal: 12" would not match, and you should not rely on that kind of luck.
Anchors
Two characters tie a pattern to the edges of the text. ^ means the start, and $ means the end.
const pattern = /^Total: 12$/;
console.log(pattern.test("Total: 12"));
console.log(pattern.test("Total: 120"));
console.log(pattern.test("Grand Total: 12"));
true
false
false
Now the whole text has to be exactly "Total: 12". This pattern does the same job as comparing with the string, so the string is simpler. Patterns earn their place when part of the text is allowed to vary.
The parts you will use
console.log(/^Total: \d+ EUR$/.test("Total: 12 EUR"));
console.log(/^Total: \d+ EUR$/.test("Total: twelve EUR"));
console.log(/^Loan #\d{4}$/.test("Loan #0042"));
console.log(/^due (today|tomorrow)$/.test("due tomorrow"));
console.log(/^dune$/i.test("Dune"));
console.log(/^\/loans\/\d+$/.test("/loans/12"));
true
false
true
true
true
true
\dis one digit.+means one or more of the thing before it. So\d+is a whole number of any length.- Letters do not match
\d. {4}means exactly four of the thing before it.(today|tomorrow)means one or the other.- The
iafter the closing slash makes the pattern ignore upper and lower case. - A slash inside a pattern is written
\/, because a plain slash would end the pattern. This one checks a path with a number at the end.
A few characters have a special meaning in a pattern: ., ?, *, +, (, ), [, ], $ and ^. To match one of them as an ordinary character, put a backslash in front of it. The dot is the one that causes bugs. It means "any one character".
console.log(/^12.50$/.test("12.50"));
console.log(/^12.50$/.test("12950"));
console.log(/^12\.50$/.test("12950"));
true
true
false
The first pattern accepts "12950", because the dot matched the 9. A price check written that way passes on a wrong price.
In a test
Assertions accept a pattern where they accept a string.
import { test, expect } from '@playwright/test';
test('the fees page shows a total', async ({ page }) => {
await page.goto('/fees');
await expect(page.getByTestId('total')).toHaveText(/^Total: \d+\.\d{2} EUR$/);
await expect(page).toHaveURL(/\/fees$/);
});
/fees is a relative path, and Playwright puts the baseURL from its configuration in front of it. The first pattern accepts any amount with two decimals. The second checks how the address ends, whatever the host is.
Notice what the first check proves. It proves the total has the right form. It does not prove the amount is right. When your test knows the amount, compare with the exact string. A pattern is for the parts you cannot know, such as an order number or a time.
In a code review, a pattern in an assertion gets one check from a careful reviewer: what wrong text would this still match? If you cannot rule out a wrong value, tighten the pattern or use a string.
Remember this
A pattern matches anywhere in the text unless you anchor it with ^ and $. A dot means any character, so escape it when you mean a dot. Use an exact string when you know the value, and a pattern only for the parts that vary.
Reference: Regular expressions in the MDN JavaScript Guide.