Skip to content

fix(regexp): escape leading digit #6208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion regexp/escape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,5 @@ export function escape(str: string): string {
return str.replaceAll(
RX_REGEXP_ESCAPE,
(m) => RESERVED_CHARS[m as keyof typeof RESERVED_CHARS],
);
).replace(/^\d/, (m) => `\\x${m.codePointAt(0)!.toString(16)}`);
}
9 changes: 9 additions & 0 deletions regexp/escape_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,12 @@ Deno.test("escape() works with all ASCII", async (t) => {
});
});
});

Deno.test("escape() escapes the leading digit", () => {
const escaped = escape("1");
assertEquals(escaped, "\\x31");

const re = new RegExp(`^${escaped}$`);
assertMatch("1", re);
assertNotMatch("a", re);
});