Relevant source files
- index.d.ts
- index.js
escapeStringRegexp API
Overview
The escapeStringRegexp function is a utility that escapes all special characters in a string for use in a regular expression.
Usage
The function takes a string as input and returns the escaped string. It is designed to be used when you need to use a string as a regular expression pattern, but the string may contain special characters that have a special meaning in regular expressions.
Key Flows
- The function iterates over each character in the input string.
- If the character is a special character in regular expressions (such as
.,*,+, etc.), it is escaped by prefixing it with a backslash (\). - If the character is not a special character, it is left unchanged.
- The escaped string is then returned.
Example
const escaped = escapeStringRegexp('Hello, world!');
console.log(escaped); // Output: "Hello\,\ world\!"
Mermaid Diagram
graph TD
A[input string] --> B[iterate over each character]
B --> C[check if character is special]
C -->|yes| D[escape character]
C -->|no| E[leave character unchanged]
E --> F[return escaped string]