Skip to content

Commit c429d96

Browse files
committed
Break up large regex
I am working on fixing a bug I found that I believe needs to be fixed by changing this regex. To make it easier to understand and work with this regex, I am breaking it into multiple lines and adding some comments.
1 parent f997a04 commit c429d96

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

index.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,26 @@ function parseGradient(str) {
1717
// match side and any corner, in browser,
1818
// `top right` and `right top` are the same,
1919
// so we should put this situation into consideration
20-
/* eslint-disable max-len */
21-
var rSideCorner = /to\s+((?:left|right|top|bottom)|(?:(?:(?:left|right)\s+(?:top|bottom))|(?:(?:top|bottom)\s+(?:left|right))))(?=\s*,)/;
20+
var rSideCorner = new RegExp(
21+
'to\\s+' +
22+
'(' + // 1
23+
'(?:left|right|top|bottom)' +
24+
'|' +
25+
'(?:' +
26+
'(?:' + // left top, left bottom, right top, right bottom
27+
'(?:left|right)\\s+(?:top|bottom)' +
28+
')' +
29+
'|' +
30+
'(?:' + // top left, top right, bottom left, bottom right
31+
'(?:top|bottom)\\s+(?:left|right)' +
32+
')' +
33+
')' +
34+
')' + // end 1
35+
'(?=\\s*,)'
36+
);
37+
2238
// match color stops, the color format is not very precise
39+
/* eslint-disable max-len */
2340
var rColorStops = /\s*(#[0-9a-f]{3,6}|(?:hsl|rgb)a?\(.+?\)|\w+)(?:\s+((?:[+-]?\d*\.?\d+)(?:%|[a-z]+)?))?/gi;
2441
// the final gradient line regexp
2542
var rGradientLine = new RegExp('^\\s*' + rAngle.source + '|' + rSideCorner.source, 'i');

0 commit comments

Comments
 (0)