@@ -2,66 +2,34 @@ class Glob {
22 constructor ( glob ) {
33 this . glob = glob
44
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' )
128 return
139 }
10+ this . regexptText = this . globize ( this . glob )
11+ this . regexp = new RegExp ( `^${ this . regexptText } $` , 'u' )
12+ }
1413
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 /
4822 }
4923
5024 toString ( ) {
5125 return this . glob
5226 }
5327
5428 [ 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 ) )
5829 return s . search ( this . regexp )
5930 }
6031
6132 [ 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 ) )
6533 return s . match ( this . regexp )
6634 }
6735
@@ -73,10 +41,4 @@ class Glob {
7341 return s . replaceAll ( this . regexp , replacement )
7442 }
7543}
76-
77- // Helper function to escape regular expression special chars
78- function escapeRegExp ( string ) {
79- return string . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' )
80- }
81-
8244module . exports = Glob
0 commit comments