DD
DevDash

Regex for Relative URL

Regex Pattern

^\/[^\s]*$

Relative URL path starting with /

Quick Answer

The regex pattern for relative url is `^\/[^\s]*$`. Relative URL path starting with /. This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.

Test Examples

InputResult
/about✓ Matches
/api/v1/users✓ Matches
/path?query=value✓ Matches
https://example.com✗ No match
relative/path✗ No match
no-slash✗ No match

Code Examples

javascript

const regex = /^\\/[^\s]*$/;
const isValid = regex.test(value);

python

import re
pattern = r'^\/[^\s]*$'
if re.match(pattern, value):
    print("valid")

ruby

pattern = /^\/[^\s]*$/
if value =~ pattern
  puts "valid"
end

php

if (preg_match('/^\\/[^\s]*$/', $value)) {
    echo "valid";
}

java

String pattern = "^\\/[^\\s]*$";
boolean isValid = value.matches(pattern);

Frequently Asked Questions

Related Regex Patterns

Want API access + no ads? Pro coming soon.