JavaScript / ECMAScript

Unix File Path in JS

Matches absolute Unix/Linux file paths.

Try it in the JS tester →

Pattern

regexJS
^(/[^/\0]+)+/?$

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("^(/[^/\\0]+)+/?$", "");
const input = "/home/user/documents";
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

`^(/[^/\0]+)+` matches one or more path segments beginning with `/`, excluding null and additional slashes within segments. `/?$` allows optional trailing slash.

Examples

Input

/home/user/documents

Matches

  • /home/user/documents

Input

/var/log/

Matches

  • /var/log/

Input

relative/path

No match

Same pattern, other engines

← Back to Unix File Path overview (all engines)