1 module.exports = function (glob, opts) {
2 if (typeof glob !== 'string') {
3 throw new TypeError('Expected a string');
6 var str = String(glob);
8 // The regexp we are building, as a string.
11 // Whether we are matching so called "extended" globs (like bash) and should
12 // support single character matching, matching ranges of characters, group
14 var extended = opts ? !!opts.extended : false;
16 // When globstar is _false_ (default), '/foo/*' is translated a regexp like
17 // '^\/foo\/.*$' which will match any string beginning with '/foo/'
18 // When globstar is _true_, '/foo/*' is translated to regexp like
19 // '^\/foo\/[^/]*$' which will match any string beginning with '/foo/' BUT
20 // which does not have a '/' to the right of it.
21 // E.g. with '/foo/*' these will match: '/foo/bar', '/foo/bar.txt' but
22 // these will not '/foo/bar/baz', '/foo/bar/baz.txt'
23 // Lastely, when globstar is _true_, '/foo/**' is equivelant to '/foo/*' when
24 // globstar is _false_
25 var globstar = opts ? !!opts.globstar : false;
27 // If we are doing extended matching, this boolean is true when we are inside
28 // a group (eg {*.html,*.js}), and false otherwise.
31 // RegExp flags (eg "i" ) to pass in to RegExp constructor.
32 var flags = opts && typeof( opts.flags ) === "string" ? opts.flags : "";
35 for (var i = 0, len = str.length; i < len; i++) {
89 // Move over all consecutive "*"'s.
90 // Also store the previous and next characters
91 var prevChar = str[i - 1];
93 while(str[i + 1] === "*") {
97 var nextChar = str[i + 1];
100 // globstar is disabled, so treat any number of "*" as one
103 // globstar is enabled, so determine if this is a globstar segment
104 var isGlobstar = starCount > 1 // multiple "*"'s
105 && (prevChar === "/" || prevChar === undefined) // from the start of the segment
106 && (nextChar === "/" || nextChar === undefined) // to the end of the segment
109 // it's a globstar, so match zero or more path segments
110 reStr += "(?:[^/]*(?:\/|$))*";
111 i++; // move over the "/"
113 // it's not a globstar, so only match one path segment
124 // When regexp 'g' flag is specified don't
125 // constrain the regular expression with ^ & $
126 if (!flags || !~flags.indexOf('g')) {
127 reStr = "^" + reStr + "$";
130 return new RegExp(reStr, flags);