https://github.com/kkirby/regexp-escape
A super simple, zero dependency library to escape regular expressions.
https://github.com/kkirby/regexp-escape
nodejs regular-expression
Last synced: 5 months ago
JSON representation
A super simple, zero dependency library to escape regular expressions.
- Host: GitHub
- URL: https://github.com/kkirby/regexp-escape
- Owner: kkirby
- License: unlicense
- Created: 2019-11-05T18:31:33.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-11-05T18:38:49.000Z (over 6 years ago)
- Last Synced: 2025-08-28T01:05:51.331Z (10 months ago)
- Topics: nodejs, regular-expression
- Language: JavaScript
- Homepage:
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# @kkirbatski/regexp-escape
A super simple, zero dependency library to escape regular expressions.
## Install
```shell
npm install @kkirbatski/regexp-escape --save-dev
```
## Usage
```javascript
const escapeRegExp = require('@kkirbatski/regexp-escape');
console.log(escapeRegExp('q[ O_O ]p'))
```
## Source
```javascript
const specialChars = [
'^',
'$',
'\\',
'.',
'*',
'+',
'?',
'(',
')',
'[',
']',
'{',
'}',
'|'
].map(specialChar => '\\' + specialChar);
const specialCharsRegExp = new RegExp(`[${specialChars.join('')}]`,'g');
module.exports = function escapeRegExpString(subject){
return subject.replace(specialCharsRegExp,'\\$&');
}
```