What edge cases are covered in the tests, especially around hyphens?
What edge cases are covered in the tests, especially around hyphens?
About sindresorhus/escape-string-regexp · 2 messages · last activity 5/25/2026
The tests cover two important edge cases related to hyphens:
-
PCRE Compatibility [test.js:L11-L15]: The test verifies that hyphens are escaped using the
\x2dhex escape format rather than a simple backslash. This is necessary for compatibility with PCRE (Perl Compatible Regular Expressions):escapeStringRegexp('foo - bar') //=> 'foo \\x2d bar' -
Unicode Flag Compatibility [test.js:L17-L21]: The test confirms that the escaped hyphen works correctly when the Unicode flag is applied to the RegExp:
new RegExp(escapeStringRegexp('-'), 'u')
The reason for these special hyphen tests becomes clear in the implementation [index.js:L9-L10]: hyphens are handled separately from other special characters, using a hex escape (\x2d) instead of a simple backslash escape. This dual approach—backslash escaping for most characters and hex escaping for hyphens—ensures the escaped string works safely in different regex contexts, particularly when the Unicode flag is used.
The main test [test.js:L3-L8] also validates that the function escapes a comprehensive set of RegExp special characters: \ ^ $ * + ? . ( ) | { } [ ].
FOLLOW_UPS:
- What characters does the function escape besides hyphens?
- Why does the function use minimal escaping instead of full context-aware escaping?
- How does this library compare to native RegExp.escape()?
Want to ask your own question?
Open chat for sindresorhus/escape-string-regexp →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.