🔍Regex Recipes

Unix Path (Basic)

Match Unix/Linux file paths for both absolute and relative paths.

Pattern

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

Explanation

Matches Unix paths: optional leading /, path segments separated by /, no null bytes.

Examples

Absolute
Input
/home/user/file.txt
Output
✓ Match
Relative
Input
relative/path/file.txt
Output
✓ Match
Current dir
Input
./file.txt
Output
✓ Match
Parent dir
Input
../parent/file.txt
Output
✓ Match
Root
Input
/
Output
✓ Match

Code Examples

JavaScript
// Node.js path module
const path = require('path');

// Resolve to absolute path
const absolute = path.resolve('./relative/path');

// Join paths safely
const joined = path.join('/home/user', 'documents', 'file.txt');
// '/home/user/documents/file.txt'

// Parse path
const parsed = path.parse('/home/user/file.txt');
// {
//   root: '/',
//   dir: '/home/user',
//   base: 'file.txt',
//   name: 'file',
//   ext: '.txt'
// }

Try it Now

💡 Tips

  • Absolute paths start with /
  • Relative paths: ./current, ../parent
  • Home directory: ~/user
  • Use path.resolve() for normalization
  • Check file system limits (255 chars per component)

⚠️ Common Pitfalls

  • Does not validate if path exists
  • Does not check permissions
  • Null bytes (\0) are forbidden
  • Use path library for production