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/6/2026
The tests cover two important edge cases around hyphens:
-
Basic hyphen escaping [test.js:L10-L14]: The test verifies that hyphens are escaped as
\x2drather than a simple backslash escape:escapeStringRegexp('foo - bar') === 'foo \\x2d bar' -
Unicode flag compatibility [test.js:L16-L20]: A second test ensures that the escaped hyphen works correctly when used with the Unicode flag:
new RegExp(escapeStringRegexp('-'), 'u')This regex should match the hyphen character.
These tests exist because hyphens have special meaning in character classes and require careful handling. The implementation uses the \x2d hexadecimal escape form [index.js:L11] rather than a simple backslash escape. According to the code comments [index.js:L7-L8], this is done because "a \xnn escape [is needed] when the simpler form would be disallowed by Unicode patterns' stricter grammar."
The README further notes [readme.md:L24] that this approach "escapes - in a way compatible with PCRE" and "compatible with the Unicode flag," which aligns with what the tests validate.
FOLLOW_UPS:
- What other RegExp special characters are escaped?
- Why is type checking important for this library?
- How does this library differ from native RegExp.escape()?
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.