RepoPilot

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

About sindresorhus/escape-string-regexp · 2 messages · last activity 7/20/2026

Question

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

RepoPilot

The tests cover two specific edge cases related to hyphens:

  1. PCRE Compatibility [test.js:L10-L14]: The test escapes - using \x2d hex notation to maintain compatibility with PCRE (Perl Compatible Regular Expressions). This is important because hyphens have special meaning in character classes and need careful handling.

  2. Unicode Flag Compatibility [test.js:L16-L20]: A separate test verifies that the escaped hyphen works correctly with the Unicode flag ('u'). The test uses t.regex() to confirm that a - character, when escaped via escapeStringRegexp('-') and used in a regex with the 'u' flag, properly matches the original hyphen.

These tests are necessary because the implementation [index.js:L10] uses \x2d (hex escape) for hyphens rather than a simple backslash escape. This dual approach handles a subtle grammar difference: the simple \\- form can be problematic in certain regex contexts, especially with stricter Unicode pattern parsing, while the hex escape \x2d works universally.

The main functionality test [test.js:L3-L7] also covers the standard RegExp special characters (\ ^ $ * + ? . ( ) | { } [ ]), showing that these are escaped with simple backslash notation.

FOLLOW_UPS:

  • Why does escape-string-regexp use hex notation for hyphens?
  • What other special characters need escaping in RegExp patterns?
  • How does this library compare to RegExp.escape() native implementation?

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.