Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mismatching issue with when path contains '[***] #277

Closed
qvandekadsye opened this issue Jul 31, 2016 · 6 comments
Closed

mismatching issue with when path contains '[***] #277

qvandekadsye opened this issue Jul 31, 2016 · 6 comments

Comments

@qvandekadsye
Copy link

Hi, I encounter some issue with matching using glob :

I'm a currently using this pattern to find my mp3 file "path.join(currentdirPath,"/*.mp3") where currentdir change thanks to a parameters.

I noticed when currentdirpath contains "[somecharacter]" like in this exemple :
Senki Zesshou Symphogear Original Soundtrack 1 [mp3], glob found nothing.
but in this exemple : Senki Zesshou Symphogear Character Song 1 - Zwei Wing

glob found all the mp3 files

i think that's beacause [...] is a pattern, but how can I escape [ and ]

@hhamilto
Copy link

how can I escape [ and ]

How about with back slashes like this:

currentdirPath.replace(/[[\]]/g,'\\$&')

That replaces [ with \[ and ] with \], which causes them to be treated as literal opening and closing bracket characters.

I imagine that there are other edge cases that should be taken care of when your path might contain special characters, but I'm not sure about how to handle all cases off the top of my head.

@hhamilto
Copy link

I was looking through the documentation just now and found a better solution to your problem, rather than escaping currentdirPath.

You should pass in the cwd property as currentdirPath on the options object when calling glob. eg:

glob('*.mp3', {cwd: currentdirPath})

@sinedied
Copy link

sinedied commented May 9, 2017

@hhamilto I have a similar issue, except that the [ and ] character are part of the filename. I tried escaping with a backslash using your regexp, but it still doesn't work. Any ideas?

@woojoo666
Copy link

woojoo666 commented Oct 8, 2018

confirmed as of today (version 7.1.3) that escaping square brackets (eg \\[) does not work. I have a suspicion it's because it supports windows-style paths, eg this is a path that glob supports: C:\\this\\is\\a\\windows\\path. However, if you try to escape brackets, eg C:\\foo \\[bar\\], then how does it differentiate between interpretting it as a path separator (equivalent to "C:/foo/[bar/]") or as an escape character (equivalent to "C:/foo [bar]")?

Also, as @sinedied noted, using the currentdirPath solution doesn't work if you are trying to match the square bracket character in the file name.

@woojoo666
Copy link

actually, as I was typing the previous comment, I came up with a devious hack: just wrap all square brackets with [...], so it gets treated as a character in the match pattern.

Eg if you want to match the path C:/foo [bar] use the match pattern C:/foo [[]bar[]]

Ugly, but works, even as far back as package version 7.1.1 (that's as far back as I checked). This should be put in the ReadMe though because it's the only solution I've found so far.

@isaacs
Copy link
Owner

isaacs commented Feb 28, 2023

The pattern c:/foo \[bar\] should work fine now in v9.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants