Escape special characters in strings for safe use in regular expressions.
escape-string-regexp is a JavaScript library that escapes special characters in strings so they can be safely used in regular expressions. It solves the problem of dynamically constructing regex patterns without unintended matches due to metacharacters. This is particularly useful when user input or variable strings need to be matched literally.
JavaScript and Node.js developers who work with regular expressions and need to safely interpolate dynamic strings into regex patterns.
It provides a simple, reliable, and minimal way to escape regex special characters, avoiding the complexity of manual escaping. While native `RegExp.escape()` is now available, this library remains a lightweight and widely-used solution for environments without that support.
Escape RegExp special characters
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Escapes only regex metacharacters like $ and ., keeping the output simple and readable, as emphasized in the README's philosophy of minimal escaping.
Enables safe insertion of dynamic strings into regex patterns, including within character classes, shown in the usage example with a Unicode emoji.
Provides guidance for complex placements of escaped strings and recommends the `regex` package for advanced needs, ensuring developers are informed about limitations.
Follows a minimalistic approach to avoid over-escaping, which maintains performance and reduces output clutter, as stated in the philosophy.
With `RegExp.escape()` now available natively in JavaScript, this library is becoming obsolete for modern projects, as noted in the README tip, reducing its necessity.
Does not account for edge cases where escaped strings might affect adjacent regex tokens, requiring careful placement by developers, as acknowledged in the README's warning.
Only applicable to JavaScript and Node.js projects, limiting its use in multi-language or cross-platform applications, unlike broader regex utilities.