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 5/18/2026
Yes, the function validates its input and throws an error on bad arguments.
In [index.js:L2-L4], the function checks if the input is a string:
if (typeof string !== 'string') {
throw new TypeError('Expected a string');
}
If you pass anything other than a string (like a number, object, null, etc.), it will throw a TypeError with the message "Expected a string". This validation happens before any escaping logic is executed.
The TypeScript definitions in [index.d.ts:L16] enforce this at compile time as well by requiring the parameter to be typed as string.
FOLLOW_UPS:
- What characters does the function escape exactly?
- Can this function handle Unicode characters safely?
- Why does the function escape hyphens differently than other characters?
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.