text.pattern-match
Does the whole value match an HTML pattern attribute? The same answer in a browser, Python and Rust.
1.0.0 · published 2026-10-10 by charlie · Anterra
Pinned by 47 tests, run in TypeScript, Python and Rust.
What it does
`matchesPattern(pattern, value)` answers what an input's `pattern` attribute answers in the browser: does the **whole** value match? The browser compiles `pattern` as `^(?:pattern)$` with the `v` flag; this does the same, in TypeScript, Python and Rust, so a server checks a submitted form with exactly the rule the browser showed. `form.validate-input` uses it for its `pattern` rule.
matchesPattern("[0-9]{5}", "90210"); // true
matchesPattern("[0-9]{5}", "90210\n"); // false: no trailing-newline slack
matchesPattern("(?=.*\\d).{8,}", "x"); // throws "unsupported pattern: ..."
For example
matchesPattern([0-9]{5}, 90210)→ true five digits: a US ZIP code matchesmatchesPattern([0-9]{5}, 9021)→ false the whole value must match: four digits of five do notmatchesPattern([0-9]{5}, 902101)→ false the whole value must match: a sixth digit is not ignored
The function
The same function in TypeScript, Python and Rust, pinned by the same tests. Pick your language; the choice follows you around the registry.
export function matchesPattern(pattern: string, value: string): boolean
| pattern | string | a regular expression in the portable subset the README lists, as an input's pattern attribute takes it |
| value | string | the text to test; the pattern must match all of it, as the browser's pattern check does |
| returns | bool | true when the whole value matches |
Your code names it in one line, in the file that uses it
import { matchesPattern } from "#fune/text.pattern-match@^1";
// A whole-string matcher for the portable subset of the regular expressions an
// HTML pattern attribute takes. The pattern is compiled to a small program and
// run as a Thompson NFA (a Pike VM without captures), so time is linear in the
// value whatever the pattern: (a+)+b cannot blow up as a backtracking engine
// would. Everything is over Unicode code points, as the browser's v-flag regex is.
const MAX_PATTERN = 1000;
const MAX_REPEAT = 1000;
const MAX_PROGRAM = 20000;
const MAX_CODE_POINT = 0x10ffff;
type Range = [number, number];
interface ClassNode { kind: "class"; ranges: Range[]; negated: boolean }
interface GroupNode { kind: "group"; alt: Node[][] }
type Atom = ClassNode | GroupNode;
interface Node { atom: Atom; min: number; max: number } // max -1: unbounded
const DIGIT: Range[] = [[48, 57]];
const WORD: Range[] = [[48, 57], [65, 90], [95, 95], [97, 122]];
const SPACE: Range[] = [[9, 13], [32, 32]];
// What `.` does not match: the line terminators, as in JavaScript.
const LINE_ENDS: Range[] = [[10, 10], [13, 13], [0x2028, 0x2029]];
const SYNTAX = "^$\\.*+?()[]{}|/";
const CLASS_SYNTAX = "()[]{}/-\\|";
const DOUBLED = "&!#$%*+,.:;<=>?@^`~";
const CLASS_PUNCTUATORS = "&-!#%,:;<=>@`~";
function unsupported(detail: string): Error {
return new Error(`unsupported pattern: ${detail}`);
}
function isAsciiDigit(c: number): boolean {
return c >= 48 && c <= 57;
}
function complement(ranges: Range[]): Range[] {
const sorted = ranges.map((r): Range => [r[0], r[1]]).sort((a, b) => a[0] - b[0]);
const out: Range[] = [];
let next = 0;
for (const [lo, hi] of sorted) {
if (lo > next) out.push([next, lo - 1]);
if (hi + 1 > next) next = hi + 1;
}
if (next <= MAX_CODE_POINT) out.push([next, MAX_CODE_POINT]);
return out;
}
class Parser {
pos = 0;
readonly cps: number[];
readonly end: number;
constructor(cps: number[], end: number) {
this.cps = cps;
this.end = end;
}
peek(): number | undefined {
return this.pos < this.end ? this.cps[this.pos] : undefined;
}
parseAlt(depth: number): Node[][] {
const alt: Node[][] = [this.parseSeq(depth)];
while (this.peek() === 124 /* | */) {
this.pos++;
alt.push(this.parseSeq(depth));
}
return alt;
}
parseSeq(depth: number): Node[] {
const seq: Node[] = [];
for (;;) {
const c = this.peek();
if (c === undefined || c === 124) return seq;
if (c === 41 /* ) */) {
if (depth === 0) throw unsupported("unbalanced )");
return seq;
}
const atom = this.parseAtom(depth);
const [min, max] = this.parseQuantifier();
seq.push({ atom, min, max });
}
}
parseAtom(depth: number): Atom {
const c = this.cps[this.pos];
const ch = String.fromCodePoint(c);
if (ch === "*" || ch === "+" || ch === "?") throw unsupported(`nothing to repeat before ${ch}`);
if (ch === "{") {
if (this.readBraces() !== null) throw unsupported("nothing to repeat before {");
throw unsupported("a lone { must be escaped as \\{");
}
if (ch === "}" || ch === "]") throw unsupported(`a lone ${ch} must be escaped as \\${ch}`);
if (ch === "^") throw unsupported("^ is only supported at the start of the pattern");
if (ch === "$") throw unsupported("$ is only supported at the end of the pattern");
if (ch === ".") {
this.pos++;
return { kind: "class", ranges: LINE_ENDS, negated: true };
}
if (ch === "(") {
this.pos++;
if (this.peek() === 63 /* ? */) {
if (this.cps[this.pos + 1] === 58 /* : */ && this.pos + 1 < this.end) this.pos += 2;
else throw unsupported("lookaround, named groups and flags are not supported");
}
const alt = this.parseAlt(depth + 1);
if (this.peek() !== 41) throw unsupported("unbalanced (");
this.pos++;
return { kind: "group", alt };
}
if (ch === "[") return this.parseClass();
if (ch === "\\") {
this.pos++;
const escaped = this.parseEscape(false);
return { kind: "class", ranges: escaped.ranges, negated: escaped.negated };
}
this.pos++;
return { kind: "class", ranges: [[c, c]], negated: false };
}
// After a backslash: a class escape, a control escape or escaped punctuation.
parseEscape(inClass: boolean): { ranges: Range[]; negated: boolean; single: number | null } {
const c = this.peek();
if (c === undefined) throw unsupported("the pattern ends with a lone \\");
this.pos++;
const ch = String.fromCodePoint(c);
switch (ch) {
case "d": return { ranges: DIGIT, negated: false, single: null };
case "D": return { ranges: DIGIT, negated: true, single: null };
case "w": return { ranges: WORD, negated: false, single: null };
case "W": return { ranges: WORD, negated: true, single: null };
case "s": return { ranges: SPACE, negated: false, single: null };
case "S": return { ranges: SPACE, negated: true, single: null };
case "t": return { ranges: [[9, 9]], negated: false, single: 9 };
case "n": return { ranges: [[10, 10]], negated: false, single: 10 };
case "v": return { ranges: [[11, 11]], negated: false, single: 11 };
case "f": return { ranges: [[12, 12]], negated: false, single: 12 };
case "r": return { ranges: [[13, 13]], negated: false, single: 13 };
}
// Escaped punctuation: what the v flag accepts, the syntax characters
// everywhere and the reserved punctuators inside [...].
if (SYNTAX.includes(ch) || (inClass && CLASS_PUNCTUATORS.includes(ch))) {
return { ranges: [[c, c]], negated: false, single: c };
}
throw unsupported(`\\${ch} is not supported (backreferences, \\b, \\p, \\u and \\x are not)`);
}
parseClass(): ClassNode {
this.pos++; // [
let negated = false;
if (this.peek() === 94 /* ^ */) {
negated = true;
this.pos++;
}
const ranges: Range[] = [];
for (;;) {
const c = this.peek();
if (c === undefined) throw unsupported("unbalanced [");
if (c === 93 /* ] */) {
this.pos++;
return { kind: "class", ranges, negated };
}
const lo = this.classAtom();
if (this.peek() === 45 /* - */) {
this.pos++;
if (this.peek() === 93) throw unsupported("- must be escaped inside [...]");
if (lo.single === null) throw unsupported("a range in [...] must run between two characters");
if (this.peek() === undefined) throw unsupported("unbalanced [");
const hi = this.classAtom();
if (hi.single === null) throw unsupported("a range in [...] must run between two characters");
if (lo.single > hi.single) throw unsupported("a range in [...] runs backwards");
ranges.push([lo.single, hi.single]);
} else if (lo.single !== null) {
ranges.push([lo.single, lo.single]);
} else {
for (const r of lo.ranges) ranges.push(r);
}
}
}
classAtom(): { ranges: Range[]; single: number | null } {
const c = this.cps[this.pos];
const ch = String.fromCodePoint(c);
if (ch === "\\") {
this.pos++;
const e = this.parseEscape(true);
return { ranges: e.negated ? complement(e.ranges) : e.ranges, single: e.single };
}
if (CLASS_SYNTAX.includes(ch)) throw unsupported(`${ch} must be escaped inside [...]`);
if (DOUBLED.includes(ch) && this.pos + 1 < this.end && this.cps[this.pos + 1] === c) {
throw unsupported(`${ch}${ch} is reserved inside [...]`);
}
this.pos++;
return { ranges: [[c, c]], single: c };
}
// {n}, {n,} or {n,m} at pos, or null (pos unchanged) when it is not one.
readBraces(): [number, number] | null {
let p = this.pos + 1;
const number = (): number | null => {
const start = p;
let n = 0;
while (p < this.end && isAsciiDigit(this.cps[p])) {
n = Math.min(n * 10 + (this.cps[p] - 48), MAX_REPEAT + 1);
p++;
}
return p === start ? null : n;
};
const min = number();
if (min === null) return null;
let max: number | null = min;
if (p < this.end && this.cps[p] === 44 /* , */) {
p++;
max = number();
if (max === null) max = -1;
}
if (p >= this.end || this.cps[p] !== 125 /* } */) return null;
this.pos = p + 1;
return [min, max];
}
parseQuantifier(): [number, number] {
const c = this.peek();
let q: [number, number];
if (c === 42) { this.pos++; q = [0, -1]; }
else if (c === 43) { this.pos++; q = [1, -1]; }
else if (c === 63) { this.pos++; q = [0, 1]; }
else if (c === 123) {
const braces = this.readBraces();
if (braces === null) throw unsupported("a lone { must be escaped as \\{");
q = braces;
if (q[0] > MAX_REPEAT || q[1] > MAX_REPEAT) throw unsupported(`a repeat count above ${MAX_REPEAT}`);
if (q[1] !== -1 && q[0] > q[1]) throw unsupported("a repeat {n,m} with n above m");
} else return [1, 1];
if (this.peek() === 63) this.pos++; // lazy: the same answer for a whole-string match
const next = this.peek();
if (next === 42 || next === 43 || next === 63 || (next === 123 && this.readBracesAhead())) {
throw unsupported("nothing to repeat: a quantifier follows a quantifier");
}
return q;
}
readBracesAhead(): boolean {
const saved = this.pos;
const found = this.readBraces() !== null;
this.pos = saved;
return found;
}
}
// The program: a character test, a fork, a jump or the match.
type Op =
| { op: "char"; ranges: Range[]; negated: boolean }
| { op: "split"; x: number; y: number }
| { op: "jmp"; x: number }
| { op: "match" };
class Compiler {
prog: Op[] = [];
push(op: Op): number {
if (this.prog.length >= MAX_PROGRAM) throw unsupported("it expands to too large a program");
this.prog.push(op);
return this.prog.length - 1;
}
alt(alt: Node[][]): void {
const jumps: number[] = [];
for (let i = 0; i < alt.length; i++) {
if (i < alt.length - 1) {
const split = this.push({ op: "split", x: 0, y: 0 });
(this.prog[split] as { x: number }).x = this.prog.length;
this.seq(alt[i]);
jumps.push(this.push({ op: "jmp", x: 0 }));
(this.prog[split] as { y: number }).y = this.prog.length;
} else {
this.seq(alt[i]);
}
}
for (const j of jumps) (this.prog[j] as { x: number }).x = this.prog.length;
}
seq(seq: Node[]): void {
for (const node of seq) {
for (let i = 0; i < node.min; i++) this.atom(node.atom);
if (node.max === -1) {
const split = this.push({ op: "split", x: 0, y: 0 });
(this.prog[split] as { x: number }).x = this.prog.length;
this.atom(node.atom);
this.push({ op: "jmp", x: split });
(this.prog[split] as { y: number }).y = this.prog.length;
} else {
const splits: number[] = [];
for (let i = node.min; i < node.max; i++) {
const split = this.push({ op: "split", x: 0, y: 0 });
(this.prog[split] as { x: number }).x = this.prog.length;
splits.push(split);
this.atom(node.atom);
}
for (const s of splits) (this.prog[s] as { y: number }).y = this.prog.length;
}
}
}
atom(atom: Atom): void {
if (atom.kind === "class") this.push({ op: "char", ranges: atom.ranges, negated: atom.negated });
else this.alt(atom.alt);
}
}
function inClass(op: { ranges: Range[]; negated: boolean }, c: number): boolean {
let hit = false;
for (const [lo, hi] of op.ranges) {
if (c >= lo && c <= hi) {
hit = true;
break;
}
}
return hit !== op.negated;
}
function addThread(prog: Op[], list: number[], seen: Uint8Array, pc: number): void {
const stack = [pc];
while (stack.length > 0) {
const p = stack.pop() as number;
if (seen[p]) continue;
seen[p] = 1;
const op = prog[p];
if (op.op === "jmp") stack.push(op.x);
else if (op.op === "split") {
stack.push(op.y);
stack.push(op.x);
} else list.push(p);
}
}
/**
* Whether the whole value matches the pattern, as an input's pattern
* attribute checks it (the browser wraps it as ^(?:pattern)$). Throws on
* anything outside the portable subset, so a pattern the server cannot check
* exactly as the browser does is caught when it is written, not in production.
*/
export function matchesPattern(pattern: string, value: string): boolean {
const cps = Array.from(pattern, (s) => s.codePointAt(0) as number);
if (cps.length > MAX_PATTERN) throw new Error(`pattern is too long: at most ${MAX_PATTERN} characters`);
let start = 0;
let end = cps.length;
if (end > 0 && cps[0] === 94 /* ^ */) start = 1;
// A trailing $ is the end anchor unless it is escaped (an odd run of backslashes before it).
if (end > start && cps[end - 1] === 36 /* $ */) {
let slashes = 0;
for (let i = end - 2; i >= start && cps[i] === 92; i--) slashes++;
if (slashes % 2 === 0) end--;
}
const parser = new Parser(cps, end);
parser.pos = start;
const alt = parser.parseAlt(0);
if (parser.pos !== end) throw unsupported("unbalanced )");
const compiler = new Compiler();
compiler.alt(alt);
compiler.push({ op: "match" });
const prog = compiler.prog;
let current: number[] = [];
addThread(prog, current, new Uint8Array(prog.length), 0);
for (const s of value) {
const c = s.codePointAt(0) as number;
const next: number[] = [];
const seen = new Uint8Array(prog.length);
for (const pc of current) {
const op = prog[pc];
if (op.op === "char" && inClass(op, c)) addThread(prog, next, seen, pc + 1);
}
current = next;
if (current.length === 0) return false;
}
return current.some((pc) => prog[pc].op === "match");
}Install
fune build
With that line in your source, in a TypeScript project (language typescript in fune.project), fune build resolves it and nothing else, pins them in fune.lock, downloads only the TypeScript package of each, and builds the code above into your project’s .fune/build, one readable file per capability with a header linking back here. Or pin a range in fune.project and build in one step:
fune add text.pattern-match
The manifest, vectors and README with only the TypeScript implementation. Install it without the registry with fune add ./text.pattern-match-1.0.0-typescript.fune, or fetch it from a terminal with fune pull text.pattern-match@1.0.0:typescript.
The whole function, every language, is one file too: text.pattern-match-1.0.0.fune, 59,895 bytes, sha256 1601403c75ad3e25b15c2e3f7d9139527e1011000f684c2bd84bfcbb196c864f. It installs into a project of any language.
Customise it in your app
The seams this capability offers. Put a marker directly above a function of your own and fune build wires it into the built code; the package on the registry is not changed, the built file’s header lists it under CUSTOMISED, and fune hooks lists every hook in the project. How hooks work.
before — your function gets the arguments and returns them, changed or not, or throws to refuse the call.
// fune: before text.pattern-match
after — your function gets the result and the arguments, and returns the final result.
// fune: after text.pattern-match
replace — it requires no other capability, so there is no dependency to replace.
step — your function runs at a numbered point inside the function’s body, receives the in-scope values it names as parameters, and may return replacements. List the points with fune show text.pattern-match --steps.
// fune: step text.pattern-match after <n|label>
Tests
A version published now needs at least 8 tests for every function, and one that expects the error for each function that throws; the registry refuses it otherwise. fune verify --all runs each case in TypeScript, Python and Rust, and a project runs them again with fune verify. This page lists the cases; it does not run them. The exact JSON is vectors.json.
| Case | Arguments | Expected | |
|---|---|---|---|
| five digits: a US ZIP code matches | [0-9]{5}, 90210 | → | true |
| the whole value must match: four digits of five do not | [0-9]{5}, 9021 | → | false |
| the whole value must match: a sixth digit is not ignored | [0-9]{5}, 902101 | → | false |
| a trailing newline is not part of a match (Python's $ would allow it) | [0-9]{5}, 90210 | → | false |
| \d is ASCII: an Arabic-Indic digit is not a digit | \d{3}, ١٢٣ | → | false |
| \d+ matches ASCII digits | \d+, 0123456789 | → | true |
| a UK mobile number with optional spaces | 07\d{3} ?\d{3} ?\d{3}, 07700 900 982 | → | true |
| alternation inside a non-capturing group | (?:GB|IE)[0-9]{2}, IE42 | → | true |
| alternation at the top level covers the whole value either way | cat|dog, catdog | → | false |
| ^ and $ at the ends are allowed and change nothing | ^[A-Z]{3}$, GBP | → | true |
Show the other 37 tests
| Case | Arguments | Expected | |
|---|---|---|---|
| an escaped $ at the end is a literal dollar | [0-9]+\$, 100$ | → | true |
| an optional letter: colour or color | colou?r, color | → | true |
| a bounded repeat {2,3} refuses four | x{2,3}, xxxx | → | false |
| {2,} has no upper bound | x{2,}, xxxxxxxx | → | true |
| a negated class with a range | [^a-z]+, ABC-123 | → | true |
| a negated class refuses what it lists | [^a-z]+, ABc | → | false |
| \w is ASCII letters, digits and underscore only | \w+, café | → | false |
| \s is ASCII whitespace | a\sb, a b | → | true |
| . matches one code point, an emoji included | a.b, a😀b | → | true |
| . does not match a newline | a.b, a b | → | false |
| a non-ASCII literal matches itself | café, café | → | true |
| an escaped dot is a literal dot | v\d+\.\d+, v1x2 | → | false |
| an escaped - inside a class is a literal hyphen | [A-Z\-]+, AB-CD | → | true |
| nested repeats that blow up a backtracking engine answer at once | (a+)+b, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaac | → | false |
| a lazy quantifier gives the same whole-string answer | a+?b, aaab | → | true |
| the empty pattern matches only the empty value | , | → | true |
| the empty pattern refuses anything else | , a | → | false |
| a star matches the empty value | [0-9]*, | → | true |
| case matters: there is no ignore-case flag | [a-z]+, Abc | → | false |
| lookahead is refused | (?=.*\d).{8,}, password1 | → | error: unsupported pattern |
| a backreference is refused | (a)\1, aa | → | error: unsupported pattern |
| \b is refused | \bword\b, word | → | error: unsupported pattern |
| a Unicode property escape is refused | \p{L}+, abc | → | error: unsupported pattern |
| a named group is refused | (?<year>\d{4}), 2026 | → | error: unsupported pattern |
| an unclosed group is refused | (ab, ab | → | error: unsupported pattern |
| an unopened group is refused | ab), ab | → | error: unsupported pattern |
| an unclosed class is refused | [a-z, a | → | error: unsupported pattern |
| a backwards range is refused | [z-a], a | → | error: unsupported pattern |
| a quantifier with nothing to repeat is refused | *a, a | → | error: unsupported pattern |
| two quantifiers in a row are refused | a**, a | → | error: unsupported pattern |
| {n,m} with n above m is refused | a{3,2}, aa | → | error: unsupported pattern |
| a repeat count above 1000 is refused | a{1001}, a | → | error: unsupported pattern |
| an unescaped hyphen at the end of a class is refused, as the browser's v flag does | [a-z-], a | → | error: unsupported pattern |
| ^ in the middle of the pattern is refused | a^b, ab | → | error: unsupported pattern |
| a pattern of exactly 1000 characters is accepted | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa… | → | true |
| repeats that expand past the program limit are refused | (?:(?:a{1000}){1000}), a | → | error: unsupported pattern |
| a pattern of 1001 characters is refused | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa… | → | error: pattern is too long |
Verification
Verified by the operator, not the sandbox 47 test vectors declared; none run by the registry.
Verified locally by the registry's operator, not by the sandbox. This version was published with FUNE_VERIFY_MODE=inline-dev (the operator's import of the first-party registry, or a development server): the server validated the package - its manifest, signatures, imports, safety lint and test rules - but the registry did not run its test vectors in its sandbox. The operator runs them with `fune verify --all` before importing.
Signed by the registry with key fw-9dc887227da47575 (ed25519), over its integrity and this report's digest. The full report · JSON
More from the author
## Why not the language's own regex
The three engines disagree. Python's `$` matches before a trailing newline, its `\d` and `\w` are Unicode (`"٣"` is a digit), and `re` has no `v` flag; Rust's standard library has no regex at all (and a crate is not allowed); JavaScript's `\s` takes Unicode spaces. A pattern checked by three engines is three rules. This is one small engine written three times, pinned by the same vectors, and checked against the browser's own `RegExp(…, "v")` by a differential fuzz while it was written (no disagreement on any pattern both accept).
## The supported subset
| Syntax | Meaning | |--------|---------| | `a`, `é`, `😀` | that code point | | `.` | any code point but a line terminator (`\n`, `\r`, U+2028, U+2029) | | `\d` `\D` | ASCII digit `[0-9]`, and not | | `\w` `\W` | ASCII word character `[A-Za-z0-9_]`, and not | | `\s` `\S` | ASCII whitespace (space, `\t`, `\n`, `\v`, `\f`, `\r`), and not | | `\t` `\n` `\v` `\f` `\r` | those control characters | | `\.` `\*` `\(` … | an escaped syntax character `^ $ \ . * + ? ( ) [ ] { } \| /` | | `[abc]` `[a-z]` `[^0-9]` | a class, with ranges, negation and the escapes above; inside a class `\-` `\&` `\!` `\#` `\%` `\,` `\:` `\;` `\<` `\=` `\>` `\@` `` \` `` `\~` are literals too | | `(…)` `(?:…)` | a group (nothing is captured: whole-string matching has no use for it) | | `a\|b` | alternation | | `*` `+` `?` `{n}` `{n,}` `{n,m}` | repeats, with a lazy `?` suffix accepted (it cannot change a whole-string answer) | | `^` first, `$` last | allowed and ignored: the match is always of the whole value |
Everything is over Unicode code points, as the `v` flag is. There is no ignore-case: list both cases (`[A-Za-z]`).
## What is refused, and why
`matchesPattern` throws `unsupported pattern: <what>` (the same phrase in every language) for:
- lookahead and lookbehind (`(?=` `(?!` `(?<=` `(?<!`), named groups, inline flags: they cannot be run in linear time, or are rarely used in a pattern attribute; - backreferences (`\1`, `\k<…>`), `\b` and `\B`, `\p{…}`, `\u…`, `\x…`, `\c…`, `\0`, and any other escaped letter or punctuation the `v` flag rejects; - `^` or `$` anywhere but the very start and end of the pattern; - an unbalanced `(`, `)`, `[`, a lone `]`, `{` or `}`; - the `v` flag's own class rules: an unescaped `( ) [ ] { } / - \ |` inside `[…]` (so `[A-Za-z-]` must be written `[A-Za-z\-]`; the browser ignores the former pattern entirely), and doubled punctuators such as `&&`; - a range written backwards (`[z-a]`) or with a class at either end (`[\d-z]`); - a quantifier with nothing before it, or straight after another (`a**`); - `{n,m}` with n above m, or a count above 1000; - a pattern that would compile to more than 20,000 steps (nested counted repeats such as `(a{1000}){1000}`).
A pattern over 1000 code points throws `pattern is too long`.
A refused pattern is a programming error, not a user's typing error: it is thrown whatever the value, so it fails the first test that uses it.
## Linear time
The pattern compiles to a program run as a Thompson NFA (a Pike VM without captures): each character of the value is looked at once against each program step, so `(a+)+b` against 10,000 `a`s answers at once. A backtracking engine takes exponential time on it, which is how a pattern on a public form becomes a denial of service on the server that re-checks it.
## Differences from the browser
Only where the browser's own rules are wider than the subset: `\s` here is ASCII whitespace, where JavaScript's also takes no-break and other Unicode spaces. Every pattern the subset accepts is one the browser accepts, and on the values tested they agree.
Files
| Path | Bytes |
|---|---|
| README.md | 4,337 |
| impl/python.py | 13,048 |
| impl/rust.rs | 16,398 |
| impl/typescript.ts | 13,779 |
| vectors.json | 8,160 |