File & Path

Unix File Path

Matches absolute Unix/Linux file paths.

Try it in RegexPro →

Available in

JavaScript / ECMAScript code

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

Raw source: ^(/[^/\0]+)+/?$

How it 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

—

Common use cases

  • •Path validation
  • •File system tools
  • •Shell scripting

Related patterns