Skip to content

Fix regex character class parsing#38

Closed
davejcameron wants to merge 1 commit into
dy:mainfrom
davejcameron:dc.regex-char-class-slashes
Closed

Fix regex character class parsing#38
davejcameron wants to merge 1 commit into
dy:mainfrom
davejcameron:dc.regex-char-class-slashes

Conversation

@davejcameron
Copy link
Copy Markdown
Contributor

Problem

Regex literals can contain / inside character classes, but the parser currently treats the first / as the end of the regex body even when it appears inside [...].

Fix

Track when the regex scanner is inside a character class, so / only terminates the literal outside of character classes. Escapes continue to consume the escaped character as part of the pattern.

Add focused regex coverage for character classes containing /.

Validation

  • Added regex parser coverage for / inside character classes

@dy
Copy link
Copy Markdown
Owner

dy commented May 18, 2026

Landed on main in eac665c.

The branch was CONFLICTING — its base predates current main and carried an unrelated /= (c === 61) guard that belongs to a separate PR and would break the existing /=/g test. So the character-class fix alone was applied directly:

let cls = false;
const pattern = next(c =>
  c === BSLASH ? 2 :
  c === LBRACK ? (cls = true, 1) :
  c === RBRACK ? (cls = false, 1) :
  c && (cls || c !== SLASH)
);

Added test('regex: character classes', ...) covering /[^/]+$/, /[\]/]/, and a .match eval case. Full suite green (336 pass, 0 fail). Closing as resolved.

@dy dy closed this May 18, 2026
dy added a commit that referenced this pull request May 18, 2026
`/` inside a `[...]` character class is literal, not a terminator.
Track a `cls` flag while scanning the pattern so `/[^/]+$/` and
`/[\]/]/` parse correctly. Closes #38.

Co-Authored-By: Claude Opus 4.7 <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.

2 participants