Skip to content

fix: remove invalid unicode escapes from regExpEscape#289

Open
abhu85 wants to merge 1 commit intoisaacs:mainfrom
abhu85:fix/unicode-escape-invalid-chars
Open

fix: remove invalid unicode escapes from regExpEscape#289
abhu85 wants to merge 1 commit intoisaacs:mainfrom
abhu85:fix/unicode-escape-invalid-chars

Conversation

@abhu85
Copy link

@abhu85 abhu85 commented Mar 2, 2026

Summary

Fixes #273 - Invalid regex generated when glob includes comma and character class.

The regExpEscape function was escaping characters like ,, #, -, and whitespace with backslash (e.g., \,). However, these escape sequences are invalid in unicode regex mode (/u flag).

When a pattern includes a POSIX character class like [[:space:]], the unicode flag is required because POSIX classes use unicode property escapes (e.g., \p{Z}). This caused patterns like ,[[:space:]] to throw:

SyntaxError: Invalid regular expression: /^\,[\p{Z}\t\r\n\v\f]$/u: Invalid escape

Changes

  • Modified regExpEscape in src/ast.ts to only escape characters that are both:

    1. Regex metacharacters (need escaping)
    2. Valid to escape in unicode mode
  • Added comprehensive test suite in test/posix-class-with-comma.js covering:

    • Comma with POSIX character classes
    • Hash character with POSIX classes
    • Space character with POSIX classes
    • Hyphen outside character class with POSIX classes
    • Multiple special characters combined

Test Plan

  • Reproduced the original issue - minimatch('foo', ',[[:space:]]') threw SyntaxError
  • Verified the fix resolves the issue - no longer throws
  • Verified correct matching behavior (comma-space matches, comma-x doesn't)
  • All existing tests pass (6249 total)
  • Updated snapshots for changed regex patterns (semantically equivalent)

Generated with Claude Code

Characters like comma, hash, hyphen, and whitespace were being escaped
with backslash in the regExpEscape function. However, these escape
sequences (e.g., \,) are invalid in unicode regex mode (/u flag).

When a pattern includes a POSIX character class like [[:space:]], the
unicode flag is required because POSIX classes use unicode property
escapes (e.g., \p{Z}). This caused patterns like ",[[:space:]]" to
throw a SyntaxError.

The fix removes these unnecessary escapes from regExpEscape. These
characters don't need escaping outside of character classes since they
are not regex metacharacters.

Fixes isaacs#273

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

Invalid regex generated when glob includes comma and character class

1 participant