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 6/15/2026
The tests cover two specific edge cases related to hyphens:
-
PCRE Compatibility [test.js:L10-L14]: The test verifies that hyphens are escaped using the hex escape sequence
\x2drather than a backslash escape. This is because a simple backslash escape of hyphens can cause issues in Perl Compatible Regular Expressions (PCRE). -
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
\x2dfor 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()?
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.