RepoPilot

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

About sindresorhus/escape-string-regexp · 2 messages · last activity 6/15/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 verifies that hyphens are escaped using the hex escape sequence \x2d rather than a backslash escape. This is because a simple backslash escape of hyphens can cause issues in Perl Compatible Regular Expressions (PCRE).

  2. Unicode Flag Compatibility [test.js:L16-L21]: The test ensures that when a hyphen is escaped using the hex escape sequence and the regex is used with the Unicode flag ('u'), it still matches correctly. This validates that the escaping approach works properly with Unicode patterns.

The general test [test.js:L3-L8] also covers escaping of core special regex characters like \ ^ $ * + ? . ( ) | { } [ ], ensuring they're properly escaped with backslashes.

The implementation [index.js:L10-L11] handles this by using two separate regex replacements:

  • First, it escapes standard regex special characters with backslashes
  • Then, it specifically handles hyphens by replacing them with \x2d for broader compatibility

FOLLOW_UPS:

  • Why does the library use hex escape for hyphens specifically?
  • What other special characters need escaping in RegExp?
  • How does this differ from 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.