, javascript:alert(1).","position":2},{"@type":"HowToStep","name":"Drop it in your code","text":"Paste the language-specific snippet (JavaScript, Python, PHP, Go, Java, Ruby, C#, Rust) into your validation logic.","position":3}],"tool":[{"@type":"HowToTool","name":"Regular Expression"}]}, javascript:alert(1) and rejects inputs like normal text,
content
."}},{"@type":"Question","name":"How do I use the Basic XSS Detection regex pattern in JavaScript?","acceptedAnswer":{"@type":"Answer","text":"Wrap the pattern in slashes: const re = /]*>.*?<\\/script>|javascript:|onerror=|onload=/; — then call re.test(value) to check a single value, or value.match(re) to find matches. Set the g flag (/.../g) to find all matches in a string."}},{"@type":"Question","name":"How do I use the Basic XSS Detection regex in Python?","acceptedAnswer":{"@type":"Answer","text":"Import re and use re.fullmatch(r\"]*>.*?<\\/script>|javascript:|onerror=|onload=\", value) for whole-string validation, or re.findall(r\"]*>.*?<\\/script>|javascript:|onerror=|onload=\", text) to extract matches. Compile with re.compile() for repeated use."}},{"@type":"Question","name":"Is the basic xss detection regex production-ready?","acceptedAnswer":{"@type":"Answer","text":"Yes — every pattern is tested against valid and invalid examples. For critical inputs, pair it with server-side validation: Luhn algorithm for credit cards, mod-97 for IBAN, real DNS / MX lookup for emails, libphonenumber for phone numbers."}},{"@type":"Question","name":"Why does the basic xss detection regex fail in Python, Java, or Go?","acceptedAnswer":{"@type":"Answer","text":"Different regex engines (ECMAScript, PCRE, java.util.regex, Python re, Go RE2) support slightly different syntax — RE2 has no lookarounds or backreferences. Use the per-language code snippets above; they escape the pattern correctly for each language."}},{"@type":"Question","name":"Can I edit and test the basic xss detection regex live?","acceptedAnswer":{"@type":"Answer","text":"Yes — use the live tester on this page. Type your test string and toggle flags (g, i, m, s, u, y) to see matches and capture groups highlighted instantly. The URL is shareable."}}]}
Securitypopular

Basic XSS Detection Regex Pattern

Detects common XSS patterns.

Pattern
<script[^>]*>.*?<\/script>|javascript:|onerror=|onload=

Tested examples

<script>alert('XSS')</script>
javascript:alert(1)
normal text
<div>content</div>

Test it live

Live Regex TesterJS
2 matches
/
/g
<script>alert('XSS')</script>
javascript:alert(1)
Match 1at index 0
<script>alert('XSS')</script>
Match 2at index 30
javascript:

Use it in your language

Use it in
// JavaScript / Node.js
const regex = /<script[^>]*>.*?<\/script>|javascript:|onerror=|onload=/;
const value = "<script>alert('XSS')</script>";
const isMatch = regex.test(value);
console.log(isMatch); // true / false

// Extract all matches
const matches = value.match(/<script[^>]*>.*?<\/script>|javascript:|onerror=|onload=/g) || [];

Tags

Frequently asked questions

How do I use the Basic XSS Detection regex pattern in JavaScript?
Wrap the pattern in slashes: const re = /<script[^>]*>.*?<\/script>|javascript:|onerror=|onload=/; — then call re.test(value) to check a single value, or value.match(re) to find matches. The "Use it in" snippets above give you the exact code for 9 languages.
Is this basic xss detection regex production-ready?
Yes — every pattern in the library is tested against valid and invalid examples. Still, regex is one layer in a defense-in-depth strategy: pair it with server-side validation (e.g. Luhn for credit cards, mod-97 for IBAN, real DNS lookup for emails) for critical inputs.
Why does my pattern fail in another language?
Different regex engines (PCRE, Java, Python, Go's RE2) support slightly different syntax. The most common gotchas: lookbehinds (not in RE2), named groups syntax, and how backslashes need to be escaped inside string literals. The code snippets above already escape correctly for each language.
Can I edit this pattern and test it live?
Yes — use the live tester above. Type your test string and toggle flags (g, i, m, s, u, y) to see matches highlighted instantly, including capture groups.

Related patterns

See all Security

Browse the full library — 300 tested regex patterns across 16 categories.