15

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.

1
  • 1
    The Grunt.js globbing patterns doc gives some explanation as to what {} and * mean, but I'm still unable to put a complete explanation as to what the {,*/}* means.
    – Tri Nguyen
    Jun 12, 2013 at 19:45

2 Answers 2

16

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.

7
  • Thank you. I am still unclear as to why the forward slash needs to be in the {}. What would something like this mean {,*}/*?
    – Tri Nguyen
    Jun 13, 2013 at 16:22
  • 3
    Hi Tri, the reason you need the / is that in globbing the * matches all characters EXCEPT an actual slash. It's set up that way so that, for example, globbing /*/*/* finds only files that are at a depth of two directories in your directory tree. {,*}/* would only match paths that do have a slash, where as {,*/}* makes the slash optional. Jun 13, 2013 at 16:31
  • thanks for that your answer. That makes sense. However, if that's the case, why won't it be {,*,/}*, basically add another comma to separate the slash from the star?
    – Tri Nguyen
    Jun 14, 2013 at 17:48
  • 1
    I should have said " {,*/}* makes the directory and trailing slash for it optional" not just "makes the slash optional". If you do {,*,/}* then you are matching either: 1. nothing 2. some characters without a slash, which would have no effect because the * after the } would already allow these characters anyway 3. just a slash -- so it would only match scripts// which would be an odd path. You need {,*/}* so that you can match scripts/anythinghere/file.coffee or scripts/file.coffee . NB this does NOT match scripts/anythinghere/somethingelsehere/file.coffee. Jun 14, 2013 at 18:19
  • 2
    Yes to the previous comment, no to the comment before that. */ means "any character besides a slash, repeated any number of times, followed by a slash." The slash has to be at the end, because that's the order in the regex. It doesn't "combine". Jun 14, 2013 at 23:04
6

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

3
  • @SindreSorhus Do you have a resource that would show us what it means? It's funny I have a -1 and the other guy with the same answer has a +1. Was that you? Jun 13, 2013 at 0:15
  • Hi Absolute... not sure what is up with Sindre -- our answers are indeed the same. I just upvoted yours. Jun 13, 2013 at 16:27
  • I also just flagged Sindre's comment as not constructive. Jun 13, 2013 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.