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.

Probing this browser…

concepts

  1. Buffer anchor tester

    The canonical surface: a live RegExp tester with flag toggles, preset inputs that contain line breaks, and the explainer’s own truth table for /\Afoo|^bar$|baz\z/um re-run in your browser.

  2. Assertion position map

    An inspection tool: for any text, see every index where \A, ^, \z, $ and \Z match, with and without m — and check each escape against its RegExp-modifiers desugaring.

  3. 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…\z while keeping per-line ^/$, and accept a file’s trailing newline with \Z.

  4. Annex B literal trap

    Edge and failure behaviour: without u/v, \A is a literal A (even the explainer’s /end\Z/ example). Plus the SyntaxErrors — inside a class, under a quantifier — and why \Z absorbs only one line break.

  5. 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

references