File tree Expand file tree Collapse file tree 4 files changed +17
-6
lines changed Expand file tree Collapse file tree 4 files changed +17
-6
lines changed Original file line number Diff line number Diff line change 11/**
22Escape 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```
68import 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
1113new RegExp(escapedString);
1214```
1315*/
14- declare const escapeStringRegexp : ( str : string ) => string ;
16+ declare const escapeStringRegexp : ( string : string ) => string ;
1517
1618export = escapeStringRegexp ;
Original file line number Diff line number Diff line change 11'use strict' ;
22
3- const matchOperatorsRegex = / [ | \\ { } ( ) [ \] ^ $ + * ? . ] / g;
3+ const matchOperatorsRegex = / [ | \\ { } ( ) [ \] ^ $ + * ? . - ] / g;
44
55module . exports = string => {
66if ( typeof string !== 'string' ) {
Original file line number Diff line number Diff line change @@ -15,12 +15,14 @@ $ npm install escape-string-regexp
1515``` js
1616const 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
2121new 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
Original file line number Diff line number Diff 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+ } ) ;
You can’t perform that action at this time.
0 commit comments