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 7/13/2026
Question
RepoPilot
Yes, the function validates its input and throws an error on bad arguments.
In [index.js:L2-L4], the function includes a type check:
if (typeof string !== 'string') {
throw new TypeError('Expected a string');
}
If you pass anything other than a string (e.g., a number, object, null, undefined), the function will throw a TypeError with the message 'Expected a string'. This happens before any escaping logic is executed, ensuring that only valid string inputs proceed to the regex replacement operations [index.js:L8-L10].
FOLLOW_UPS:
- What characters does this function escape?
- Can you use the output in all regex contexts safely?
- What's the difference between the two replacement strategies?
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.