🔍Regex Recipes

Windows Path (Basic)

Match Windows file paths with drive letter and backslashes. Includes escaping examples.

Pattern

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

Explanation

Matches Windows paths: drive letter, colon, backslashes, excluding invalid filename characters.

Examples

Absolute path
Input
C:\\Users\\John\\file.txt
Output
✓ Match
Folder path
Input
D:\\Projects\\App\\
Output
✓ Match
Invalid - colon in name
Input
C:\\bad:name.txt
Output
✗ No match

Code Examples

JavaScript
// In JavaScript strings, use double backslash
const path = 'C:\\Users\\John\\Documents\\file.txt';

// Path module (Node.js) is better
const path = require('path');
const normalized = path.normalize('C:/Users/John/file.txt');
// Returns: 'C:\\Users\\John\\file.txt'

// Extract components
const parsed = path.parse('C:\\Users\\file.txt');
// {
//   root: 'C:\\',
//   dir: 'C:\\Users',
//   base: 'file.txt',
//   name: 'file',
//   ext: '.txt'
// }

Try it Now

💡 Tips

  • Use path module in Node.js for path operations
  • Forward slashes (/) also work in most Windows APIs
  • Normalize paths for comparison
  • Check path length limits (260 chars traditional limit)

⚠️ Common Pitfalls

  • Backslashes must be escaped in regex and strings
  • Invalid characters: \ / : * ? " < > |
  • UNC paths (\\\\server\\share) need different pattern
  • Use path library for production