Regex for Path Traversal Pattern
Regex Pattern
(?:\.\.\/|\.\.\\)Detects directory traversal attempts (../ or ..\)
Quick Answer
The regex pattern for path traversal pattern is `(?:\.\.\/|\.\.\\)`. Detects directory traversal attempts (../ or ..\). This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.
Test Examples
| Input | Result |
|---|---|
| ../etc/passwd | ✓ Matches |
| ..\windows\system32 | ✓ Matches |
| ../../secret | ✓ Matches |
| /normal/path | ✗ No match |
| file.txt | ✗ No match |
| ./current-dir | ✗ No match |
Code Examples
javascript
const regex = /(?:\.\.\\/|\.\.\\)/; const isValid = regex.test(value);
python
import re
pattern = r'(?:\.\.\/|\.\.\\)'
if re.match(pattern, value):
print("valid")ruby
pattern = /(?:\.\.\/|\.\.\\)/ if value =~ pattern puts "valid" end
php
if (preg_match('/(?:\.\.\\/|\.\.\\)/', $value)) {
echo "valid";
}java
String pattern = "(?:\\.\\.\\/|\\.\\.\\\\)"; boolean isValid = value.matches(pattern);
Frequently Asked Questions
Related Regex Patterns
SQL Injection Pattern
Basic SQL injection detection patterns
XSS Script Tag
Detects HTML script tags (basic XSS detection)
Email Address
Validates a standard email address format
Email (RFC 5322 Compliant)
RFC 5322 compliant email validation with label length limits
URL (HTTP/HTTPS)
Validates HTTP and HTTPS URLs
URL (Any Protocol)
Matches URLs with any protocol (http, https, ftp, ws, etc.)