REGEXVAULTv2.0
Security/Injection Patterns
Verified Safe

SQL Injection Pattern (Basic Detection) Regex for JavaScript

/(?:;|--|#|/\*|\*/|\bUNION\b|\bSELECT\b|\bINSERT\b|\bUPDATE\b|\bDELETE\b|\bDROP\b|\bCREATE\b|\bALTER\b|\bEXEC\b|\bEXECUTE\b|\bxp_|\bsp_|'(?:--|OR|AND)\s+|OR\s+1\s*=\s*1|AND\s+1\s*=\s*1|'\s*OR\s*'|"\s*OR\s*"|CHAR\s*\(|CONCAT\s*\(|SLEEP\s*\(|WAITFOR\s+DELAY)/i

What this pattern does

This page provides a comprehensive, battle-tested regular expression for matching sql injection pattern (basic detection), ported and verified for JavaScript. 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 JavaScript project — whether you're validating in an Express middleware, a Next.js API route, or a client-side form.

Javascript Implementation

Javascript
// SQL Injection Pattern (Basic Detection)
// ReDoS-safe | RegexVault — Security > Injection Patterns

const sqlInjectionPatternBasicDetectionRegex = /(?:;|--|#|\/\*|\*\/|\bUNION\b|\bSELECT\b|\bINSERT\b|\bUPDATE\b|\bDELETE\b|\bDROP\b|\bCREATE\b|\bALTER\b|\bEXEC\b|\bEXECUTE\b|\bxp_|\bsp_|'(?:--|OR|AND)\s+|OR\s+1\s*=\s*1|AND\s+1\s*=\s*1|'\s*OR\s*'|"\s*OR\s*"|CHAR\s*\(|CONCAT\s*\(|SLEEP\s*\(|WAITFOR\s+DELAY)/i;

function validateSqlInjectionPatternBasicDetection(input: string): boolean {
  return sqlInjectionPatternBasicDetectionRegex.test(input);
}

// Example
console.log(validateSqlInjectionPatternBasicDetection("' OR 1=1--")); // true

Test Cases

Matches (Valid)
Rejects (Invalid)
' OR 1=1--hello world
1; DROP TABLE users;--John's Coffee Shop
UNION SELECT password FROM users
' OR '1'='1

When to use this pattern

This pattern is drawn from the Security > Injection Patterns category and carries a ReDoS-safe certification. That matters for JavaScript developers because especially critical in long-running Node.js event loops where a ReDoS vulnerability can block the entire process. 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

Regex-based SQL injection detection is insufficient as a primary defense — it cannot keep up with encoding variations (Unicode, URL encoding, hex encoding). The only reliable defense is parameterized queries (prepared statements) with an ORM.

Technical Notes

Detection pattern for WAF rules and input validation logging. HIGH false positive rate — legitimate inputs can match (e.g., user names with apostrophes, technical documentation). Use for alerting/logging rather than hard blocking. Always use parameterized queries as the real defense.

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