Web
Percent-Encoded URL Escape Regex
URLs encode reserved or unsafe characters as a percent sign followed by two hexadecimal digits, so a space becomes %20 and a slash becomes %2F. This pattern finds each such escape, which helps when checking whether a string is already encoded, counting encoded bytes, highlighting escapes in a debugging tool or detecting double encoding such as %2520. A bare percent sign or one followed by non-hex characters does not match. The honest limit is that multi-byte UTF-8 characters appear as several separate escapes, so decoding still needs a proper URL decoding function.
Pattern (JavaScript)
/%[0-9A-Fa-f]{2}/Matches the character "%", then a hexadecimal digit, exactly 2 times.
Examples
Matches
- ✓ Hello%20World
- ✓ a%2Fb
- ✓ %3d
Does not match
- ✗ 100% sure
- ✗ %G1
- ✗ %2
Try it
3 matches (JavaScript engine)
Hello%20World a%2Fb %3d 100% sure %G1 %2
Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /%[0-9A-Fa-f]{2}/g;
const text = "Hello%20World";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'%[0-9A-Fa-f]{2}')
text = 'Hello%20World'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/%[0-9A-Fa-f]{2}/';
$text = 'Hello%20World';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("%[0-9A-Fa-f]{2}");
String text = "Hello%20World";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`%[0-9A-Fa-f]{2}`)
text := "Hello%20World"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"%[0-9A-Fa-f]{2}");
var text = "Hello%20World";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"%[0-9A-Fa-f]{2}").unwrap();
let text = "Hello%20World";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /%[0-9A-Fa-f]{2}/
text = 'Hello%20World'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- How do I detect double encoding?
- Look for %25 followed by two hex digits, for example with %25[0-9A-Fa-f]{2}. That means a percent sign itself was encoded, which usually indicates a value was encoded twice.
- Is %3d the same as %3D?
- Yes, the hex digits are case-insensitive, and both represent the equals sign. The URL standard recommends uppercase, so normalise if you compare encoded strings.
- Can I decode escapes with a regex replace?
- You can replace each match with the character for its hex value, but multi-byte UTF-8 sequences need to be decoded together. Built-in functions like decodeURIComponent handle that correctly.