File & Path

Windows File Path

Matches absolute Windows file paths (e.g., C:\Users\file.txt).

Try it in RegexPro →

Available in

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("^[A-Za-z]:\\\\(?:[^\\\\/:*?\"<>|\\r\\n]+\\\\)*[^\\\\/:*?\"<>|\\r\\n]*$", "");
const input = "C:\\Users\\John\\file.txt";
const match = input.match(re);
console.log(match ? match[0] : null);

Uses `String.prototype.match`, which returns the first match or null.

Pattern

regexengine-agnostic
^[A-Za-z]:\\(?:[^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*$

Raw source: ^[A-Za-z]:\\(?:[^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*$

How it works

`^[A-Za-z]:\\` matches the drive letter and root backslash. `(?:[^\\/:*?"<>|\r\n]+\\)*` matches directory segments. Final segment (the file or folder name) is allowed to be empty.

Examples

Input

C:\Users\John\file.txt

Matches

  • C:\Users\John\file.txt

Input

D:\

Matches

  • D:\

Input

/unix/path

No match

—

Common use cases

  • •Cross-platform tools
  • •File pickers
  • •Path validation

Related patterns