v160 · javascript · regexp
Regular Expression Buffer Boundaries
Perl, PCRE, .NET, ICU and Oniguruma have had them for decades: \A matches the start of the whole input, \z the very end, and \Z the end allowing for one trailing line terminator — and none of them care about the m flag. This TC39 proposal brings all three to Unicode-mode (u / v) JavaScript regular expressions as readable synonyms for (?-m:^), (?-m:$) and (?=(?:\r\n|\n|\r|
|
)?(?-m:$)), so one pattern can anchor to lines and to the buffer at once.
concepts
-
Buffer anchor tester
The canonical surface: a live
RegExptester with flag toggles, preset inputs that contain line breaks, and the explainer’s own truth table for/\Afoo|^bar$|baz\z/umre-run in your browser. -
Assertion position map
An inspection tool: for any text, see every index where
\A,^,\z,$and\Zmatch, with and withoutm— and check each escape against its RegExp-modifiers desugaring. -
Multiline validation bypass
The practical reason it matters:
/^[a-z.-]+$/m“validates” a hostname allowlist that has an injected line. Bound the buffer with\A…\zwhile keeping per-line^/$, and accept a file’s trailing newline with\Z. -
Annex B literal trap
Edge and failure behaviour: without
u/v,\Ais a literalA(even the explainer’s/end\Z/example). Plus the SyntaxErrors — inside a class, under a quantifier — and why\Zabsorbs only one line break. -
Portable pattern import
Advanced composition: import PCRE/TextMate-style patterns from a JSON config, feature-detect, transpile to modifier groups as a clearly labelled fallback, scan a document with them, and drive an
<input pattern>that is silently ignored when unsupported.
the API
// Unicode mode (u or v) is required
/\Afoo\z/u.test("foo"); // true
/\Afoo\z/um.test("foo\nbar"); // false — m does not affect \A / \z
// lines AND buffer in one pattern
const re = /\Afoo|^bar$|baz\z/um;
re.test("\nbar"); // true (^ is per-line under m)
re.test("\nfoo"); // false (\A is start of input only)
re.test("baz\n"); // false (\z is the absolute end)
// \Z: end of input, optionally after ONE line terminator
/end\Z/u.test("The end\r\n"); // true
/end\Z/u.test("The end\n\n"); // false
// without u: Annex B identity escapes — literal "A"
/\Afoo/.test("Afoo"); // true (!)
research notes
- Exact surface: three new Assertion productions,
[+UnicodeMode] \A,\z,\Z. No new methods or properties — detection is “doesnew RegExp("\\A", "u")compile and behave”. - Semantics:
\Asucceeds only at index 0;\zonly at the input length;\Zwhere an optional\r\n,\n,\r, U+2028 or U+2029 is followed by the end. All are zero-width and cannot be quantified or used inside a character class. - Flag (V8):
js_regexp_buffer_boundaries, listed in V8’s shipped-features list on main. To try it on an older Chrome or Canary build, launch with--js-flags=--js-regexp-buffer-boundariesor--enable-features=V8Flag_js_regexp_buffer_boundaries(both verified on Chrome 153 here). There is nochrome://flagsentry, and neither#enable-javascript-harmonynor--enable-experimental-web-platform-featuresturns it on (verified). - Interop: Firefox ships it (155 release notes, per ChromeStatus); Safari has no signal yet. Tests: test262 PR #4975.
- Relationship to modifiers: the escapes are sugar over RegExp modifiers (Chrome 125+), which is what makes an honest transpiled fallback possible — see the last concept.