How does this function validate its input — does it throw on bad arguments?
How does this function validate its input — does it throw on bad arguments?
About sindresorhus/escape-string-regexp · 2 messages · last activity 6/1/2026
Question
RepoPilot
Yes, the function does validate its input and throws on bad arguments.
In [index.js:L2-L4], the function includes explicit type checking:
if (typeof string !== 'string') {
throw new TypeError('Expected a string');
}
This means if you pass anything that isn't a string (like a number, object, null, etc.), the function will throw a TypeError with the message "Expected a string". After this validation passes, the function proceeds to escape RegExp special characters in the string.
FOLLOW_UPS:
- What characters does this function escape?
- Can I use escaped strings in character classes?
- How does it handle the hyphen character differently?
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.