Defensive programming

Bounded regex validation

Try the free JavaScript compiler

Check type and size before matching a simple fixed allowlist; bounded input is not itself proof of safe worst-case performance.

Formal Syntax & Structure

javascript syntaxjavascript
typeof value === "string" && value.length <= limit && pattern.test(value)

Working Example

Example Snippetjavascript
const s="AB12"; console.log(s.length<=12 && /^[A-Z][A-Z0-9]*$/.test(s));
JavaScript Reference