RepoPilot

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

About sindresorhus/escape-string-regexp · 2 messages · last activity 7/27/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 verifies that hyphens are escaped as \x2d (hexadecimal escape) rather than a simple backslash escape. This is specifically for compatibility with PCRE (Perl Compatible Regular Expressions).

  2. Unicode flag compatibility [test.js:L16-L20]: The test confirms that the escaped hyphen works correctly when the Unicode flag (u) is used in a RegExp. This ensures the escaping strategy is compatible with Unicode patterns' stricter grammar rules.

These tests align with the implementation in [index.js:L10], where hyphens are handled specially:

.replace(/-/g, '\\x2d');

The README also notes this distinction: [readme.md:L25-L26] mentions that the library uses "a simple backslash escape when it's always valid, and a \xnn escape when the simpler form would be disallowed by Unicode patterns' stricter grammar."

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

FOLLOW_UPS:

  • Why does the hyphen need hexadecimal escaping?
  • What other characters are escaped with simple backslashes?
  • How does the escaping handle Unicode characters like emoji?

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.