Skip to content

Prevent NPCG handling from overwriting sticky flag RegExp setup#1520

Open
Br1an67 wants to merge 1 commit intozloirock:masterfrom
Br1an67:fix/issue-810-regexp-sticky-flag
Open

Prevent NPCG handling from overwriting sticky flag RegExp setup#1520
Br1an67 wants to merge 1 commit intozloirock:masterfrom
Br1an67:fix/issue-810-regexp-sticky-flag

Conversation

@Br1an67
Copy link

@Br1an67 Br1an67 commented Mar 8, 2026

Fixes #810

Summary

This PR fixes a critical bug where the NPCG (Non-Participating Capturing Group) handling was unconditionally overwriting the reCopy variable that had been set up for sticky flag handling. This caused XRegExp patterns to break on IE11 and other browsers where both UNSUPPORTED_Y and NPCG_INCLUDED conditions are true.

Root Cause

In packages/core-js/internals/regexp-exec.js, lines 71-93, there were two separate if blocks:

  1. Sticky block (lines 71-89): Sets up reCopy for sticky flag emulation
  2. NPCG block (lines 91-93): Unconditionally sets reCopy for NPCG handling

When both conditions were true (like on IE11), the NPCG block would overwrite the sticky block's work, corrupting the RegExp pattern and breaking XRegExp patterns that used \s character classes.

Changes

  • Changed line 91 from if (NPCG_INCLUDED) to else if (NPCG_INCLUDED)
  • This ensures the NPCG block only runs when sticky handling is NOT active
  • The fix is minimal and surgical - only 1 line changed, 2 lines removed

Testing

The fix addresses the exact issue reported:

  • XRegExp patterns with \s character classes will no longer be corrupted
  • Patterns that were breaking in core-js@3.6+ will now work correctly
  • The fix maintains backward compatibility with existing functionality

Files Changed

packages/core-js/internals/regexp-exec.js | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)

Impact

  • Fixes broken XRegExp patterns on IE11 and other browsers with incomplete RegExp support
  • Resolves the endless loop issue reported in the issue
  • Maintains all existing functionality for browsers that don't have both conditions true

The sticky flag implementation and NPCG (Non-Participating Capturing Group)
handling were both unconditionally setting reCopy, causing the NPCG block
to overwrite the sticky block's work on browsers like IE11 where both
conditions are true.

This broke XRegExp patterns that used \s character classes, as the NPCG
overwrite corrupted the pattern structure that sticky handling had set up.

Changed the NPCG block to only run when sticky handling is not active.
@johnzhou721
Copy link
Contributor

Impact

  • Fixes broken XRegExp patterns on IE11 and other browsers with incomplete RegExp support
  • Resolves the endless loop issue reported in the issue
  • Maintains all existing functionality for browsers that don't have both conditions true

Might wanna add these lines into the changelog.md file, being more specific than just "resolves the endless loop issue".

@johnzhou721
Copy link
Contributor

Also I think we should add some tests.

Copy link
Owner

@zloirock zloirock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you cover this with tests?

@johnzhou721
Copy link
Contributor

@Br1an67 ? Are you interested in continuing this?

@Br1an67 Br1an67 force-pushed the fix/issue-810-regexp-sticky-flag branch from 9fe0fdc to 7630659 Compare March 18, 2026 00:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[RegExp] Incomplete sticky flag support implementation / core-js@3.6+ breaks xregexp@4.3- patterns on IE11

3 participants