DD
DevDash

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

InputResult
../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

Want API access + no ads? Pro coming soon.