RepoPilot

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

About sindresorhus/escape-string-regexp · 2 messages · last activity 6/15/2026

Question

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

RepoPilot

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

In [index.js:L2-L4], the function checks the input type:

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

If you pass anything that isn't a string to escapeStringRegexp(), it will throw a TypeError with the message "Expected a string". This ensures the function only processes string values, since the regex replacements that follow depend on string methods.

FOLLOW_UPS:

  • What special characters does the function escape?
  • Why does the function use \x2d for hyphens?
  • Can you show an example of the escaped output?

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.