@@ -2,66 +2,34 @@ class Glob {
2
2
constructor ( glob ) {
3
3
this . glob = glob
4
4
5
- // For patterns without any wildcards, match them anywhere in the string
6
- const hasWildcards = glob . includes ( '*' ) || glob . includes ( '?' )
7
-
8
- const hasNothingToEscape = escapeRegExp ( glob ) === glob
9
-
10
- if ( hasNothingToEscape ) {
11
- this . regexp = new RegExp ( `\\b${ glob } \\b` , 'u' )
5
+ // If not a glob pattern then just match the string.
6
+ if ( ! this . glob . includes ( '*' ) ) {
7
+ this . regexp = new RegExp ( `.*${ this . glob } .*` , 'u' )
12
8
return
13
9
}
10
+ this . regexptText = this . globize ( this . glob )
11
+ this . regexp = new RegExp ( `^${ this . regexptText } $` , 'u' )
12
+ }
14
13
15
- if ( ! hasWildcards ) {
16
- // Simple case: no wildcards, just do a simple substring match
17
- this . regexp = new RegExp ( escapeRegExp ( glob ) , 'u' )
18
- return
19
- }
20
-
21
- // Handle wildcard patterns
22
- let pattern
23
-
24
- if ( glob . includes ( '**' ) ) {
25
- // Handle ** which can match across directory boundaries
26
- pattern = glob
27
- . replace ( / \\ / g, '\\\\' )
28
- . replace ( / \* \* / g, '__GLOBSTAR__' )
29
- . replace ( / \. / g, '\\.' )
30
- . replace ( / \/ / g, '\\/' )
31
- . replace ( / \? / g, '.' )
32
- . replace ( / \* / g, '[^\\/]*' )
33
- . replace ( / _ _ G L O B S T A R _ _ / g, '.*' )
34
- } else {
35
- // Handle patterns with * but not **
36
- pattern = glob
37
- . replace ( / \\ / g, '\\\\' )
38
- . replace ( / \. / g, '\\.' )
39
- . replace ( / \/ / g, '\\/' )
40
- . replace ( / \? / g, '.' )
41
- . replace ( / \* / g, '[^\\/]*' )
42
- }
43
-
44
- // Handle character classes
45
- pattern = pattern . replace ( / \\ \[ ( [ ^ \] ] + ) \\ \] / g, '[$1]' )
46
-
47
- this . regexp = new RegExp ( `^${ pattern } $` , 'u' )
14
+ globize ( glob ) {
15
+ return glob
16
+ . replace ( / \\ / g, '\\\\' ) // escape backslashes
17
+ . replace ( / \/ / g, '\\/' ) // escape forward slashes
18
+ . replace ( / \. / g, '\\.' ) // escape periods
19
+ . replace ( / \? / g, '([^\\/])' ) // match any single character except /
20
+ . replace ( / \* \* / g, '.+' ) // match any character except /, including /
21
+ . replace ( / \* / g, '([^\\/]*)' ) // match any character except /
48
22
}
49
23
50
24
toString ( ) {
51
25
return this . glob
52
26
}
53
27
54
28
[ Symbol . search ] ( s ) {
55
- console . log ( 'regex patttern is ' , this . regexp )
56
- console . log ( 'string to search is ' , s )
57
- console . log ( 'string search result is ' , s . search ( this . regexp ) )
58
29
return s . search ( this . regexp )
59
30
}
60
31
61
32
[ Symbol . match ] ( s ) {
62
- console . log ( 'regex patttern is ' , this . regexp )
63
- console . log ( 'string to match is ' , s )
64
- console . log ( 'string match result is ' , s . match ( this . regexp ) )
65
33
return s . match ( this . regexp )
66
34
}
67
35
@@ -73,10 +41,4 @@ class Glob {
73
41
return s . replaceAll ( this . regexp , replacement )
74
42
}
75
43
}
76
-
77
- // Helper function to escape regular expression special chars
78
- function escapeRegExp ( string ) {
79
- return string . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' )
80
- }
81
-
82
44
module . exports = Glob
0 commit comments