Saltar al contenido principal

Rewrite a catastrophic pattern safely

Refactor a vulnerable nested-quantifier regex into a safer bounded pattern.

regular-expressions5 min read

A profile-name validator has nested repetition and optional spaces inside the repeated group, creating many ways to parse long near-misses. Remove ambiguity, bound repetition, cap input The pattern /^([a-z]+\s?)*$/ can divide the same run of letters into many group iterations before it finally rejects a hostile suffix. State the real rule 1 to 5 lowercase words separated by single spaces A safe rewrite starts with the domain rule, not the old syntax. Remove nested ambiguity ^[a-z]+(?: [a-z]+){0,4}$ The delimiter sits between words, so the engine has fewer equivalent partitions to explore. Cap input first Reject names longer than 60 characters…

Read the full lesson

Sign up free: one personalized lesson every day, matched to your role and goals.

← Back to library

Contáctanos