Skip to content

compiler: stateful /g and /y regular expression literals inside template array/object literals are memoized in pure functions #71956

Closed as not planned

Description

@halcyon-past

Which @angular/* package(s) are the source of the bug?

compiler

Is this a regression?

No

Description

Template regex optimization deliberately skips regular expression literals with stateful flags (g and y) using SAFE_REGEX_FLAGS in regular_expression_optimization.ts, because calling .test() or .exec() mutates .lastIndex.

When used directly (e.g. {{ /hello/g.test(value) }}), the compiler emits the literal inline so each evaluation gets a fresh RegExp instance.

However, RegularExpressionLiteralExpr.isConstant() unconditionally returns true without checking this.flags. As a result, when a /g or /y regular expression appears inside an array or object literal in a template, generatePureLiteralStructures treats the entry as constant and hoists the structure into a 0-argument pure function (ɵɵpureFunction0), caching and reusing the exact same RegExp instance across checks.

Because .test() advances .lastIndex on /g and /y regexes (e.g. from 0 to 5 after matching 'hello'), reusing the memoized instance causes subsequent evaluations on the same input to return false. In dev mode, this throws NG0100: ExpressionChangedAfterItHasBeenCheckedError on the first detectChanges() pass (between the initial check and checkNoChanges); in production, results flip between true and false across change detection runs.

Please provide a link to a minimal reproduction of the bug

Minimal component reproduction (verified locally in packages/core/test/acceptance/integration_spec.ts via bazel test //packages/core/test/acceptance:acceptance):

import {ChangeDetectionStrategy, Component} from '@angular/core';

@Component({
  template: '{{ [/hello/g][0].test(value) }}|{{ {re: /hello/y}.re.test(value) }}',
  changeDetection: ChangeDetectionStrategy.Eager,
})
export class App {
  value = 'hello';
}

Expected rendered output after detectChanges(): true|true (and remaining true|true on subsequent detectChanges() calls).

Please provide the exception or error you saw

Error: NG0100: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'true'. Current value: 'false'. Expression location: App component. Find more at https://next.angular.dev/errors/NG0100
    at throwErrorIfNoChangesMode (./packages/core/src/render3/errors.ts:85:9)
    at bindingUpdated (./packages/core/src/render3/bindings.ts:68:9)
    at bindingUpdated2 (./packages/core/src/render3/bindings.ts:89:21)
    at interpolation2 (./packages/core/src/render3/instructions/interpolation.ts:82:21)
    at ɵɵtextInterpolate2 (./packages/core/src/render3/instructions/text_interpolation.ts:112:24)
    at App_Template (ng:///App.js:8:17)

Please provide the environment you discovered this bug in (run ng version)

@angular/compiler: 22.3.0-next.4 (main @ 0dafb368349f6d2c436296801f2f642747899d2d)
Node: v26.10.0
OS: darwin arm64

Anything else?

Related prior change: commit 636cc9410523b7a78ed0360c720ae44713746170 introduced SAFE_REGEX_FLAGS in regular_expression_optimization.ts, whereas RegularExpressionLiteralExpr.isConstant() still returns true regardless of flags.

Activity

  1. added
    area: compiler
    on Nov 11, 2026
  2. added this to the needsTriage milestone on Nov 11, 2026
  3. halcyon-past commented on Nov 11, 2026

    @halcyon-past
    Author

    If this looks like a valid bug after triage, I'd be happy to work on a fix and submit a PR with tests for it.

  4. atscott commented on Nov 11, 2026

    @atscott
    Contributor

    Not an issue worth addressing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: compiler

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions