XSS Payload Pattern (Basic Detection) Regex for Java
/(?:<script[\s>]|<\/script>|javascript:|vbscript:|onload\s*=|onerror\s*=|onclick\s*=|onmouseover\s*=|<iframe\b|<object\b|<embed\b|<svg[\s>]|document\.cookie|document\.write|eval\s*\(|setTimeout\s*\(|setInterval\s*\(|window\.location|alert\s*\(|confirm\s*\(|prompt\s*\(|data:text/html)/iWhat this pattern does
This page provides a comprehensive, battle-tested regular expression for matching xss payload pattern (basic detection), ported and verified for Java. In security-sensitive code, using an unverified regex can open the door to both false positives and denial-of-service attacks. The snippet below is ready to drop into your Java project — whether you're validating in a Spring Boot controller, a Jakarta EE service, or a standalone utility class.
Java Implementation
// XSS Payload Pattern (Basic Detection)
// ReDoS-safe | RegexVault — Security > Injection Patterns
import java.util.regex.Pattern;
public class XssPayloadPatternBasicDetectionValidator {
private static final Pattern PATTERN =
Pattern.compile("(?:<script[\\s>]|<\\/script>|javascript:|vbscript:|onload\\s*=|onerror\\s*=|onclick\\s*=|onmouseover\\s*=|<iframe\\b|<object\\b|<embed\\b|<svg[\\s>]|document\\.cookie|document\\.write|eval\\s*\\(|setTimeout\\s*\\(|setInterval\\s*\\(|window\\.location|alert\\s*\\(|confirm\\s*\\(|prompt\\s*\\(|data:text/html)");
public static boolean validate(String input) {
return PATTERN.matcher(input).matches();
}
// Example
public static void main(String[] args) {
System.out.println(validate("<script>alert(1)</script>")); // true
}
}Test Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
<script>alert(1)</script> | Hello World |
javascript:void(0) | This is a normal comment |
onload=malicious() | <b>Bold text</b> |
<svg onload=alert(1)> | onclick is a word |
document.cookie | — |
When to use this pattern
This pattern is drawn from the Security > Injection Patterns category and carries a ReDoS-safe certification. That matters for Java developers because critical in Java applications since the JVM regex engine uses backtracking and is susceptible to ReDoS without careful pattern design. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.
Common Pitfalls
There are over 1000 known XSS filter bypass techniques. Regex-based XSS filtering in input validation is cat-and-mouse. Use DOMPurify for sanitizing user HTML, and rely on CSP (Content Security Policy) as the final layer of defense.
Technical Notes
Basic XSS detection. Incomplete — XSS payloads can be encoded in dozens of ways (HTML entities, Unicode escapes, UTF-7, etc.). Use as a signal for logging/alerting. The real defense is output encoding via a template engine (React, Vue, Jinja2 with auto-escaping).
Have a pattern that belongs in the vault?
Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.
Submit a Pattern