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 7/20/2026
The tests cover two specific edge cases related to hyphens:
-
PCRE Compatibility [test.js:L10-L14]: The test escapes
-using\x2dhex 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. -
Unicode Flag Compatibility [test.js:L16-L20]: A separate test verifies that the escaped hyphen works correctly with the Unicode flag (
'u'). The test usest.regex()to confirm that a-character, when escaped viaescapeStringRegexp('-')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?
Want to ask your own question?
Open chat for sindresorhus/escape-string-regexpPublic 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.