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/13/2026
The tests cover two specific edge cases related to hyphens:
-
PCRE Compatibility [test.js:L10-L14]: The test
'escapes-in a way compatible with PCRE'verifies that hyphens are escaped using the hex escape sequence\x2drather than a simple backslash escape. This is important because in PCRE (Perl Compatible Regular Expressions), a hyphen can have special meaning in certain contexts. -
Unicode Flag Compatibility [test.js:L16-L21]: The test
'escapes-in a way compatible with the Unicode flag'ensures that the escaped hyphen works correctly when using the Unicode (u) flag in a RegExp. This validates that\x2dis compatible with Unicode patterns.
The main test [test.js:L3-L8] also covers the general case of escaping all RegExp special characters (\ ^ $ * + ? . ( ) | { } [ ]) with simple backslash escapes.
The reason for the special hyphen handling is explained in the implementation [index.js:L6-L10]. The code uses:
.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&')for standard special characters.replace(/-/g, '\\x2d')specifically for hyphens to ensure compatibility with both PCRE and Unicode patterns
FOLLOW_UPS:
- Why does the hyphen need special
\x2descaping? - Does this library handle all RegExp special characters?
- What does the README say about edge case placements?
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.