RepoPilot

What edge cases are covered in the tests, especially around hyphens?

About sindresorhus/escape-string-regexp · 2 messages · last activity 6/1/2026

Question

What edge cases are covered in the tests, especially around hyphens?

RepoPilot

The tests cover two important edge cases related to hyphens:

  1. PCRE Compatibility [test.js:L10-L14]: The function escapes hyphens (-) as \x2d rather than a simple backslash escape. This is done to ensure compatibility with PCRE (Perl Compatible Regular Expressions), where a hyphen can have special meaning in certain contexts.

  2. Unicode Flag Compatibility [test.js:L16-L20]: There's a test verifying that the escaped hyphen works correctly when used with the Unicode (u) flag. The test uses t.regex() to confirm that a hyphen escaped by escapeStringRegexp('-') matches the literal hyphen character when the regex pattern is created with the Unicode flag enabled.

The reason for using \x2d instead of a simple \- escape is explained in the implementation [index.js:L7-L8]. The comment notes that this hex escape form is used "when the simpler form would be disallowed by Unicode patterns' stricter grammar."

The main character escaping [index.js:L9] handles the standard RegExp special characters (|\\{}()[\]^$+*?.) with simple backslash escaping, but the hyphen gets special treatment with the hex escape in a separate replacement [index.js:L10].

FOLLOW_UPS:

  • What RegExp special characters are escaped with backslash?
  • Why does the function use minimal escaping approach?
  • How does this library handle non-string input?

Public Q&A. Generated by RepoPilot from the actual source of sindresorhus/escape-string-regexp. AI answers can be incomplete or stale — verify before relying on them.