Skip to content

Commit f35e493

Browse files
committed
improve regex to support escaping
1 parent cb6927a commit f35e493

File tree

4 files changed

+92
-61
lines changed

4 files changed

+92
-61
lines changed

.verb.md

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,19 @@
1-
# {%= name %} {%= badge("fury") %} {%= badge("travis") %}
2-
3-
> {%= description %}
4-
5-
Originally [posted here](https://github.com/less/less.js/issues/2339#issuecomment-67211009).
6-
7-
## Install
8-
{%= include("install-npm", {save: true}) %}
9-
101
## Usage
112

123
```js
134
var regex = require('{%= name %}');
14-
('var foo = \'bar\';').match(regex());
5+
console.log(regex().exec('var foo = \'bar\';')[2]);
156
//=> ['bar']
167

17-
('var foo = "bar";').match(regex());
18-
//=> ['"bar"'']
19-
20-
('var foo = "bar";\nvar bar = \'baz\'').match(regex());
21-
//=> ['"bar"', '\'baz\'']
22-
23-
('foo bar ". // \' \\ . // \' \\ ." baz').match(regex());
24-
//=> ['". // \' \\ . // \' \\ ."']
25-
```
26-
27-
## Related projects
28-
{%= related(verb.related.list) %}
29-
30-
## Running tests
31-
{%= include("tests") %}
32-
33-
## Contributing
34-
{%= include("contributing") %}
35-
36-
## Author
37-
{%= include("author") %}
8+
console.log(regex().exec('var foo = "bar";')[2]);
9+
//=> ['bar']
3810

39-
## License
40-
{%= copyright() %}
41-
{%= license() %}
11+
console.log(regex().exec('var foo = "bar";\nvar bar = \'baz\'')[2]);
12+
//=> ['bar']
4213

43-
***
14+
console.log('var foo = "one";\nvar bar = \'two\';\nvar baz = `three`'.match(regex()));
15+
//=> [ '"one"', '\'two\'', '`three`' ]
4416

45-
{%= include("footer") %}
17+
console.log(regex().exec('foo bar ". // \' \\ . // \' \\ ." baz')[2]);
18+
//=> ['. // \' \\ . // \' \\ .']
19+
```

examples.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var regex = require('./');
2+
3+
console.log(regex().exec('var foo = \'bar\';')[2]);
4+
//=> ['bar']
5+
6+
console.log(regex().exec('var foo = "bar";')[2]);
7+
//=> ['bar']
8+
9+
console.log(regex().exec('var foo = "bar";\nvar bar = \'baz\'')[2]);
10+
//=> ['bar']
11+
12+
console.log('var foo = "one";\nvar bar = \'two\';\nvar baz = `three`'.match(regex()));
13+
//=> [ '"one"', '\'two\'', '`three`' ]
14+
15+
console.log(regex().exec('foo bar ". // \' \\ . // \' \\ ." baz')[2]);
16+
//=> ['. // \' \\ . // \' \\ .']

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*!
22
* quoted-string-regex <https://github.com/regexps/quoted-string-regex>
33
*
4-
* Copyright (c) 2015-2017, Jon Schlinkert.
4+
* Copyright (c) 2015-2018, Jon Schlinkert.
55
* Released under the MIT License.
66
*/
77

88
'use strict';
99

1010
module.exports = function() {
11-
return /'([^'\\]*\\.)*[^']*'|"([^"\\]*\\.)*[^"]*"/g;
11+
return /(?:\\['"`].)*?(['"`])((?:\\\1|.)*?)(\1)/g;
1212
};

test.js

Lines changed: 64 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,81 @@
1+
/*eslint quotes: [0]*/
12
'use strict';
23

34
require('mocha');
45
var assert = require('assert');
56
var regex = require('./');
67

78
describe('regex', function() {
8-
it('should return a function:', function() {
9-
assert(typeof regex === 'function');
9+
describe('single quotes', function() {
10+
it('should match single-quoted strings', function() {
11+
var match = `var foo = 'bar';`.match(regex());
12+
assert.equal(match[0], `'bar'`);
13+
});
14+
15+
it('should match empty single-quoted strings', function() {
16+
var match = `var foo = '';`.match(regex());
17+
assert.equal(match[0], `''`);
18+
});
1019
});
1120

12-
it('should match single-quoted strings:', function() {
13-
var re = regex();
14-
var str = 'var foo = \'bar\';';
15-
var m = str.match(re);
16-
assert(m[0] === '\'bar\'');
21+
describe('double quotes', function() {
22+
it('should match double-quoted strings', function() {
23+
var match = 'var foo = "bar";'.match(regex());
24+
assert.equal(match[0], '"bar"');
25+
});
26+
27+
it('should match empty double-quoted strings', function() {
28+
assert.equal('var foo = "";'.match(regex())[0], '""');
29+
assert.equal('var foo = "\'\'";'.match(regex())[0], '"\'\'"');
30+
assert.equal(regex().exec('var foo = "\'\'";')[2], '\'\'');
31+
});
1732
});
1833

19-
it('should match double-quoted strings:', function() {
20-
var re = regex();
21-
var str = 'var foo = "bar";';
22-
var m = str.match(re);
23-
assert(m[0] === '"bar"');
34+
describe('backticks', function() {
35+
it('should match strings in backticks', function() {
36+
var match = 'var foo = `${bar}`;'.match(regex());
37+
assert.equal(match[0], '`${bar}`');
38+
});
39+
40+
it('should match empty strings in backticks', function() {
41+
assert.equal('var foo = ``;'.match(regex())[0], '``');
42+
assert.equal('var foo = `""`;'.match(regex())[0], '`""`');
43+
assert.equal(regex().exec('var foo = `\'\'`;')[2], '\'\'');
44+
});
2445
});
2546

26-
it('should match multiple quoted strings:', function() {
27-
var re = regex();
28-
var str = 'var foo = "bar";\nvar bar = \'baz\'';
29-
var m = str.match(re);
30-
assert(m[0] === '"bar"');
31-
assert(m[1] === '\'baz\'');
47+
describe('multiple', function() {
48+
it('should match multiple quoted strings', function() {
49+
var match = `var foo = "one";\nvar bar = 'two'\nvar baz = \`three\``.match(regex());
50+
assert.equal(match[0], '"one"');
51+
assert.equal(match[1], `'two'`);
52+
assert.equal(match[2], `\`three\``);
53+
});
3254
});
3355

34-
it('should match complex nested quotes:', function() {
35-
var re = regex();
36-
var str = 'foo bar ". // \' \\ . // \' \\ ." baz';
37-
var m = str.match(re);
38-
assert(m[0] === '". // \' \\ . // \' \\ ."');
56+
describe('escaping', function() {
57+
it('should work with escaped quotes', function() {
58+
var double = `var foo = "bar\\"baz";`.match(regex());
59+
assert.equal(double[0], '"bar\\"baz"');
60+
61+
var single = `var foo = 'bar\\'baz';`.match(regex());
62+
assert.equal(single[0], `'bar\\'baz'`);
63+
64+
var both = `var foo = '"bar\\'\\"\\'baz"';`.match(regex());
65+
assert.equal(both[0], `'"bar\\'\\"\\'baz"'`);
66+
});
67+
68+
it('should match complex nested quotes', function() {
69+
var double = `foo bar ". // ' \\" \\ . // ' \\ ." baz`.match(regex());
70+
assert.equal(double[0], `". // ' \\" \\ . // ' \\ ."`);
71+
72+
var single = `foo bar '. // \\' \\" \\ . // \\' \\ .' baz`.match(regex());
73+
assert.equal(single[0], `'. // \\' \\" \\ . // \\' \\ .'`);
74+
});
75+
76+
it('should create a match for the inner string', function() {
77+
var inner = regex().exec(`foo bar \\" \\' '. // \\' \\" \\ . // \\' \\ .' \\'\\' baz`);
78+
assert.equal(inner[2], '. // \\\' \\" \\ . // \\\' \\ .');
79+
});
3980
});
4081
});

0 commit comments

Comments
 (0)