Reference · Regular expressions

re.test

signature
re.test(str, str, str | null=) -> bool

What it does

Whether a pattern matches anywhere in the text: true or false, nothing about where. The pattern is a string (in a string a backslash is written twice: "\\d+"), and the flags come last: "i" for any case, "m" for ^ and $ at each line (a line ending at \n or \r, \r\n as one end, in the browser too), "s" for . matching line breaks too. Patterns take only what the server and the browser read alike, so a component gets the same answer.

Examples

print(re.test("order 42", "\\d+"), re.test("order", "\\d+"))print(re.test("Hello", "^hello$"), re.test("Hello", "^hello$", "i"))print(re.test("ann@example.com", "^[\\w.]+@[\\w.]+\\.\\w+$"))
output
true falsefalse truetrue

Edge cases

print(re.test("a\nb", "^b$"), re.test("a\nb", "^b$", "m"), re.test("a\nb", "a.b"), re.test("a\nb", "a.b", "s"))print(re.test("", ""), re.test("abc", ""), re.test("", "^$"), re.test("abc", "^$"))print(re.test("añ", "^\\w+$"), re.test("٣", "\\d"), re.test("año", "a.o"))print(re.test("abc", "b", null))
output
false true false truetrue true true falsefalse false truetrue

Errors

A pattern that is not supported is a runtime error, not an error value, and quillo check reports it before the program runs when the pattern is written in the call: lookahead and lookbehind, backreferences (\1, \k<name>), \p{...}, an escape of a character that needs none (\,), a count over 1,000, an unbalanced parenthesis, a flag that is not i, m or s, or one given twice. A text or a pattern that is not a string is a runtime error too.

this one fails
print(re.test("abc", "(?<=a)b"))
output
line 1: `re.test`: lookahead and lookbehind ((?= (?! (?<= (?<!) are not supported
this one fails
print(re.test("abab", "(ab)\\1"))
output
line 1: `re.test`: backreferences (\1, \k<name>) are not supported
this one fails
print(re.test("abc", "b", "x"))
output
line 1: `re.test`: the flags are `i` (any case), `m` (^ and $ at each line) and `s` (. matches line breaks), each once; got "x"

Chaining

In a pipeline the text comes first, so re.test reads as a predicate; with filter it keeps the lines that match:

print("2026-10-29" | re.test("^\\d{4}-\\d{2}-\\d{2}$"))print(["ann@x.io", "bob", "cy@y.org"] | filter((s) => re.test(s, "@")))
output
true["ann@x.io", "cy@y.org"]

See also

re.find, re.find_all, re.replace, re.split, contains, starts_with

Thread
10Useful? Like it, or add to its thread below.

Thread 0 posts

On re.test(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo