glob file matching rules

Glob is a wildcard syntax used to match file names or paths in a file system

*

Matches 0 or more characters in the file, but does not match delimiters in the path unless the path delimiter appears at the end

|-style
	|-file1.txt
	|-file2.css
	|-subdir
		|-file3
		|-...

./style/*

matches ./style/file1.txt, ./style/file2.css, and ./style/subdir/, but not the ./style/subdir directory any content below.

To match all files and subdirectories under the ./style/ directory, including the contents of subdirectories, you can use recursive wildcards**, such as ./style/** or ./style/**/*.

./style/*.ts

Match./styleAll ts files in the directory

/style/*.*

matches /style/file1.txt, /style/file2.css because these files have file extensions. The pattern will not match /style/subdir/ or /style/subdir/file3 because they are directories rather than direct files, or do not have an explicit file extension.

./style/*/*.js

./style/subdir1/file1.js
./style/subdir1/file2.js
./style/subdir2/file3.js
./style/demo/subdir1/file1.js

matches ./style/subdir1/file1.js, ./style/subdir1/file2.js, and ./style/subdir2/file3.js because they are all in direct subdirectories .jsfile. It will not match ./style/demo/subdir1/file1.js, can only match one level of subdirectories


**

Zero or more directories and their subdirectories in the matching path need to appear alone, that is, there must be no other things around it. If it appears at the end, it can also match the file

./style/**

Match everything in the style directory, no matter how deep it is.

This pattern is typically used to recursively find all files in a directory or perform batch operations, since it matches all files and directories in a directory tree.

./style/**/*

Match all files and subdirectories under the ./style/ directory, including everything in subdirectories, as well as the contents of deeper subdirectories. This is a recursive wildcard that matches all files and directories under the entire directory tree.

./style/**/*.js

./style/file1.css
./style/file2.js
./style/subdir1/file3.css
./style/subdir1/file4.js
./style/subdir2/

array./style/file1.js, ./style/file2.js, ./style/subdir1/file3.js, ./style/subdir1/file4.js, and so on. there is some degree of depthtext,./style/subdir2/What are the contents of the article.js

This mode is typically used to find all JavaScript files in a project, including files nested in subdirectories.

a/**b/z

Matches paths that meet the following criteria:

  1. Starts with a/: The path must start with the a/ string.
  2. contains any number of subdirectories (**): ** matches zero or more directories (including subdirectories).
  3. followed by b: The path must contain a b character.
  4. ends with /z: The path must end with /z.

Therefore, this expression can match paths like a/b/z, a/x/y/b/z as long as they meet the above conditions. The main feature of this wildcard expression is to match a path starting with a/, containing a b, and then ending with /z, with optional Contains any number of subdirectories.

The following paths cannot be matched:

  1. The path does not start with a/: If the path does not start with a/, then the matching conditions are not met and the match will not succeed.
  2. The path does not end with /z: If the path does not end with /z, it does not meet the matching conditions and will not match successfully.

Here are some examples of mismatches:

  • b/z: The path does not start with a/.
  • a/x/y/c/z: Although the path contains a/, the intervening subdirectory has a c instead of a b.
  • a/b/z/c: Although the path starts with a/ and ends with /z, b is not a direct character in the path, it Must follow immediately after **.

In short, a/**b/z will only match a/, containing a b, and then < A path ending with a i=4> can contain any number of subdirectories, but is required to follow . If these conditions are not met, the expression will not match successfully. /zb**


?

Matches a character, does not match path separators

?.js

matches filenames or filenames in paths that end with .js and the first character of the filename is any character (a single character)

For example: a.js, 1.js, x.js but it will not match file names like abc.js, myjs.js


[...]

is an array composed of multiple rules. It can match files that match any subitem in the array. When the first character in the subitem is ! or ^, it means the rule does not match

./style/a[0-3].js

IndividualstyleEyefula0.js, a1.js, a2.js, a3.js

[xyz].js

can only matchx.js,y.js,z.js, but will not matchxy.js ,xyz.js, etc., the entire square bracket only represents one character

[^xyz].js

Can match a.js, b.js, c.js, etc., but cannot match x.js, y.js, z.js

Note: Another advantage of using arrays is that you can easily use exclusion patterns. Add ! before a single matching pattern in the array to form an exclusion pattern. It will Exclude this match from the matching results. One thing to note is that you cannot use the exclude pattern on the first element in the array

[*.js,!b*.js]

Match all js files, but exclude js files starting with b

['!b*.js',*.js]

No files will be excluded because the exclude pattern cannot appear in the first element of the array


{...}

Expansion mode, expands into multiple rules based on the content inside, and can match all expanded rules. Expand the above example

['./**/*.{html, php}', '!{build, simple, images, node_modules}/**']

selects all files ending with .html or .php, but excludes files named build, Directories for simple, images and node_modules and their contents. This is typically used to select a collection of files to process so that appropriate actions can be performed during the build process.


!(pattern|pattern|pattern)

This refers to excluding all files that match these patterns.

./style/!(test|login).js

matches and excludes all js files after login.js and test.js

['./**/!(_)*.{html, php}', '!{build, node_modules}/**']

Selects all files ending with .html or .php, but excludes html and file names starting with _ php files, and exclude directories named build and node_modules and their contents. This is typically used to select a set of files to process so that appropriate actions can be performed during the build process


?(pattern|pattern|pattern)

Match any pattern given in parentheses 0 or 1 times, similar to (pattern|pattern|pattern) in js regular?

./style/?(a|a2|b).js

matches ./style/, followed by a, a2, or b, and finally File path ending with .js, for example:

  • ./style/a.js
  • ./style/a2.js
  • ./style/b.js

Other paths that do not satisfy this pattern will not be matched


@(pattern|pattern|pattern)

Match any one of multiple patterns, similar to (pattern|pattern|pattern) in js regular

./style/@(a|b|c).js

  • ./style/: The path must start with ./style/.
  • @(a|b|c): This part means selecting one of the strings, that is, matching any of a, b or c one.
  • .js: The path must end with .js.

matches any one of a, b or c, ending with .js The file path at the end, for example:

  • ./style/a.js
  • ./style/b.js
  • ./style/c.js

These paths will match successfully. However, if the characters in the path are not any of a, b, or c, or do not begin with < a i=4> ends, then the match will not succeed. For example, or will not match. .js./style/d.js./style/x.js

cannot be combined, matches once, and cannot be empty. Note the difference from ?. If it is ?, it is zero or one character


+(pattern|pattern|pattern)

Match any pattern given in the brackets one or more times. These patterns can be combined together for matching, similar to (pattern|pattern|pattern)+ in js regular.

./style/+(a|a2|b).js

matches something starting with ./style/, followed by one or more characters, and can be a, a2, or Any character in a>. Example:b, and the file path ending with .js

  • ./style/ab.js
  • ./style/aa2.js
  • ./style/bbb.js

These paths match successfully because they match one or more characters and can be a, a2 or < Any character in a i=3>, and the condition ending with . b.js


*(pattern|pattern|pattern)

Match any pattern given in parentheses 0 or more times. These patterns can be combined together for matching, similar to (pattern|pattern|pattern)* in js regular.

./style/*(a|b|c).js

matches ./style/, followed by zero or more characters, and can be a, b, or < Any character in /span>. Here are some examples of successful matches:c, and the file path ending with .js

  • ./style/a.js
  • ./style/abc.js
  • ./style/bbc.js
  • ./style/ccc.js

These paths will match because they match zero or more characters and can be a, b, or Any character in c, and a condition ending with .js.

Guess you like

Origin blog.csdn.net/weixin_60053942/article/details/134195514