Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple regex in Gruntfile

Tags:

gruntjs

glob

I have the following line in my Gruntfile.

js files: ['<%= yeoman.app %>/scripts/{,*/}*.coffee'],

Could someone be so kind as tell me what {,*/}* mean? I know it's trying to match all .coffee files in the scripts folder, but I want to know how it does that.

like image 257
Tri Nguyen Avatar asked Jun 12 '13 19:06

Tri Nguyen


2 Answers

The {,*/} matches one or zero directories between scripts and the .coffee file. Inside the {} there are actually two matching terms separated by a comma. One is blank, represented by no characters to the left of the comma. One is any number of characters and a forward slash. The final * matches the filename before the .coffee extension.

By the way, this is not regular expressions, it's globbing.

like image 167
foobarbecue Avatar answered Oct 31 '22 00:10

foobarbecue


According to the documentation:

Also, because this is JavaScript, you're not limited to JSON; you may use any valid JavaScript here. You may even programmatically generate the configuration if necessary.

It looks like {,*/}* is a JSON match for:

{
 '' = empty space matches no character
 , = or
 * = Any Characters (except slash) (wildcard)
 / = literal slash
}
* = Any Characters (except slash) (wildcard)

Update Found another resource:

Globbing patterns

It is often impractical to specify all source filepaths individually, so Grunt supports filename expansion (also know as globbing) via the built-in node-glob and minimatch libraries.

While this isn't a comprehensive tutorial on globbing patterns, know that in a filepath:

* matches any number of characters, but not /

? matches a single character, but not /

** matches any number of characters, including /, as long as it's the only thing in a path part

{} allows for a comma-separated list of "or" expressions

! at the beginning of a pattern will negate the match

like image 31
AbsoluteƵERØ Avatar answered Oct 31 '22 00:10

AbsoluteƵERØ