JavaScript / ECMAScript

Windows File Path in JS

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

Try it in the JS tester →

Pattern

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

JavaScript / ECMAScript code

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

Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).

How the pattern 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

Same pattern, other engines

← Back to Windows File Path overview (all engines)