Match a Windows file path
Match an absolute Windows file path — a drive letter, colon and backslash, then one or more segments that avoid the characters Windows filenames forbid.
Last updated
Matches
- C:\\Users\\ada\\Documents\\report.docx
- open D:\\Projects\\app\\src\\index.ts now
Doesn’t match
- /usr/local/bin/app is a unix path
- just plain text C without a colon
Why it’s written this way
A drive-letter path always starts [A-Za-z]:\\ — a single letter, a colon, then a literal backslash (doubled here because the pattern is stored as a plain string, so \\\\ in the snippet is one escaped backslash for the regex engine). Each folder segment is a run of characters excluding backslash, forward slash and the characters Windows reserves — : * ? " < > | plus carriage return and newline — followed by another backslash, repeated for however many folders deep the path goes, with a final segment left unterminated for the file name.
Excluding the reserved character set instead of positively listing allowed characters means normal filenames with spaces, dots and parentheses all match without special-casing them.
Edge cases to know
- →It accepts forward slashes nowhere, so a mixed-style path like C:/Users/ada, which Windows APIs generally tolerate, will only match up through the drive letter and colon.
- →It does not check that the drive letter actually exists or that intermediate segments are valid folder names beyond excluding reserved characters — a segment of just dots or trailing spaces, which Windows itself rejects, still matches here.
- →UNC paths like \\\\server\\share are a different shape entirely — two leading backslashes instead of a drive letter — and are not covered.