RepoPilot

How does this function validate its input — does it throw on bad arguments?

About sindresorhus/escape-string-regexp · 2 messages · last activity 5/25/2026

Question

How does this function validate its input — does it throw on bad arguments?

RepoPilot

Yes, the function validates its input and throws on bad arguments.

In [index.js:L2-L4], the escapeStringRegexp function includes a type check:

if (typeof string !== 'string') {
    throw new TypeError('Expected a string');
}

If you pass anything other than a string (such as a number, object, null, or undefined), the function will throw a TypeError with the message "Expected a string". This validation happens before any escaping logic is performed.

FOLLOW_UPS:

  • What characters does this function actually escape?
  • How are hyphens treated differently from other characters?
  • Can you use the escaped output in any regex context?

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.