Testing regular expressions: include what must not match

Finding one match is a useful start, not proof that a pattern captures your intention. Good tests include text that looks almost right but must be rejected.

State the rule in ordinary language first

Are you finding a code inside a paragraph or validating an entire line? A pattern can locate a valid-looking fragment inside an invalid input and appear successful while missing your actual requirement.

Use small examples that clarify characters, length and boundaries. A long input makes the reason for success or failure harder to see, and a pattern cannot enforce business rules you never included.

Flags and writing systems affect the result

The g flag asks for multiple matches, while i changes case handling. JavaScript’s \w is not a general definition of words across writing systems; Unicode property escapes with u can be needed for broader letter matching.

ToolRoof tests JavaScript syntax and the flags g, i, m, s and u. Positions are UTF-16 units, not always counts of visible symbols. Do not assume identical behavior when moving a pattern into another regex engine.

References: MDN — JavaScript regular expressions, flags and Unicode

Passing examples do not certify safety

Include a non-matching case and examples at the intended minimum and maximum boundaries. Also check multiline input if that is where the pattern will be used.

Processing is bounded and the tool displays up to 200 matches; reaching that cap does not establish that there are no more. It does not certify a pattern’s speed or safety on every input, run replacement code or execute submitted text.

Example: two accepted codes and two near misses

In ID-17 ID-204 ID-7 XID-99, the pattern \bID-\d{2,3}\b with g finds ID-17 and ID-204. ID-7 is too short, and the word boundary prevents a match starting inside XID-99.

Without g, only the first match is returned. This rule describes the sample codes; it does not prove that identifiers exist, grant access to them or validate any other requirement of your system.

References