Relevant source files
- package.json
- readme.md
Getting Started with escape-string-regexp
Installation
To use escape-string-regexp in your projects, you need to install it as a dependency. Run the following command in your terminal:
npm install escape-string-regexp
Usage
After installation, you can import the escapeStringRegexp function in your JavaScript code:
const escapeStringRegexp = require('escape-string-regexp');
Key Flows
The escapeStringRegexp function takes a string as input and returns the string with all special characters escaped for use in regular expressions.
For example:
const input = 'Hello, world!';
const escaped = escapeStringRegexp(input);
console.log(escaped); // Output: "Hello\, world\!"
Example Use Case
You can use escapeStringRegexp to create a regular expression pattern from a user-input string:
const userInput = 'Hello, world!';
const pattern = new RegExp(escapeStringRegexp(userInput));
console.log(pattern); // Output: /Hello\, world\!/
graph TD A[User input] -->|escapeStringRegexp|> B[Escaped string] B -->|new RegExp|> C[Regular expression pattern] C -->|match|> D[Match result]
Sources: package.json readme.md