Skip to content

fix(schema): escape path.sep before use in RegExp character class on Windows#4

Merged
codezz merged 1 commit into
remember-md:mainfrom
LunarLaurus:fix/windows-path-sep-regex
May 24, 2026
Merged

fix(schema): escape path.sep before use in RegExp character class on Windows#4
codezz merged 1 commit into
remember-md:mainfrom
LunarLaurus:fix/windows-path-sep-regex

Conversation

@LunarLaurus
Copy link
Copy Markdown
Contributor

@LunarLaurus LunarLaurus commented May 13, 2026

Problem

On Windows, path.sep is \ (backslash). When interpolated directly into a RegExp character class:

new RegExp(`^Projects\${path.sep}([^${path.sep}]+)\${path.sep}(.+)$`)

the string passed to RegExp on Windows becomes:

^Projects\([^\]+)\(.+)$

Inside the character class, \] is interpreted as an escaped ] (a literal ] character), so the class [^ never closes — Node throws SyntaxError: Invalid regular expression … Unterminated character class at the point schema.js is first required, which crashes any command that invokes the validator on Windows.

Fix

Escape the separator before building the RegExp, so it is safe in both the character class and the surrounding pattern on all platforms:

const sep = path.sep.replace(/\/g, '\\');
const projMatch = rel.match(new RegExp(`^Projects${sep}([^${sep}]+)${sep}(.+)$`));
  • Windows: path.sep = '\' → sep = '\' → regex pattern [^\] (not a backslash) ✅
  • POSIX: path.sep = '/' → sep = '/' (no-op) → regex pattern [^/] ✅

Testing

Validated against Notes/*.md belief files on Windows 10 schema validator now returns clean JSON without crashing.

…Windows

On Windows, path.sep is '\'. When interpolated directly into a RegExp
character class as [^\], the backslash escapes the closing bracket,
leaving the class unterminated and throwing a SyntaxError at startup.

Escape the separator before building the RegExp so it works on both
platforms: path.sep.replace(/\/g, '\\') produces '\' on Windows
(matching a literal backslash in the pattern) and is a no-op on POSIX.
@codezz codezz merged commit 46d18a3 into remember-md:main May 24, 2026
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