Skip to content

Commit e76291d

Browse files
committed
Also escape -
This enables you to escape a string that is inserted into a regex, for example, into a character class. Fixes #9
1 parent ea98098 commit e76291d

File tree

4 files changed

+17
-6
lines changed

4 files changed

+17
-6
lines changed

index.d.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
/**
22
Escape RegExp special characters.
33
4+
You can also use this to escape a string that is inserted into the middle of a regex, for example, into a character class.
5+
46
@example
57
```
68
import escapeStringRegexp = require('escape-string-regexp');
79
8-
const escapedString = escapeStringRegexp('how much $ for a 🦄?');
9-
//=> 'how much \\$ for a 🦄\\?'
10+
const escapedString = escapeStringRegexp('How much $ for a 🦄?');
11+
//=> 'How much \\$ for a 🦄\\?'
1012
1113
new RegExp(escapedString);
1214
```
1315
*/
14-
declare const escapeStringRegexp: (str: string) => string;
16+
declare const escapeStringRegexp: (string: string) => string;
1517

1618
export = escapeStringRegexp;

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const matchOperatorsRegex = /[|\\{}()[\]^$+*?.]/g;
3+
const matchOperatorsRegex = /[|\\{}()[\]^$+*?.-]/g;
44

55
module.exports = string => {
66
if (typeof string !== 'string') {

readme.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ $ npm install escape-string-regexp
1515
```js
1616
const escapeStringRegexp = require('escape-string-regexp');
1717

18-
const escapedString = escapeStringRegexp('how much $ for a 🦄?');
19-
//=> 'how much \\$ for a 🦄\\?'
18+
const escapedString = escapeStringRegexp('How much $ for a 🦄?');
19+
//=> 'How much \\$ for a 🦄\\?'
2020

2121
new RegExp(escapedString);
2222
```
2323

24+
You can also use this to escape a string that is inserted into the middle of a regex, for example, into a character class.
25+
2426

2527
## License
2628

test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,10 @@ test('main', t => {
77
'\\\\ \\^ \\$ \\* \\+ \\? \\. \\( \\) \\| \\{ \\} \\[ \\]'
88
);
99
});
10+
11+
test('escapes `-`', t => {
12+
t.is(
13+
escapeStringRegexp('foo - bar'),
14+
'foo \\- bar'
15+
);
16+
});

0 commit comments

Comments
 (0)