RepoPilot

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

About sindresorhus/escape-string-regexp · 2 messages · last activity 8/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 test 'escapes - in a way compatible with PCRE' verifies that hyphens are escaped as \x2d rather than a simple backslash escape. This is because in PCRE (Perl-Compatible Regular Expressions), a hyphen has special meaning and requires this specific hex escape format.

  2. Unicode flag compatibility [test.js:L16-L20]: The test 'escapes - in a way compatible with the Unicode flag' confirms that the escaped hyphen works correctly when the RegExp is created with the 'u' (Unicode) flag, ensuring the escaping doesn't break Unicode regex patterns.

The implementation handles this in [index.js:L10], where after escaping standard special characters with backslashes, it applies a second replace specifically for hyphens: .replace(/-/g, '\\x2d'). This two-step approach is documented in the code comments [index.js:L6-L7], explaining that hex escapes are used "when the simpler form would be disallowed by Unicode patterns' stricter grammar."

The main test [test.js:L3-L8] also covers the standard RegExp special characters: \ ^ $ * + ? . ( ) | { } [ ], all of which receive standard backslash escaping.

FOLLOW_UPS:

  • Why does the function use two separate replace calls?
  • What characters are considered RegExp special characters?
  • How does this compare to native RegExp.escape()?

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.