Regex Builder & Parser
Prerequisites & Dependencies
- Node.js 18+ or Python 3.10+ with regex engine knowledge
npm i redoS (for safety scoring) or Python's re module + rstr for generation
- Comfortable with character classes, quantifiers, grouping, and anchors
Execution Steps
- Clearly define the input format and extraction goal: what substrings, delimiters, or patterns must be captured
- Sketch the regex on paper first: anchors (
^, $), delimiters, optional groups, alternation (|)
- Build the regex incrementally, testing each component against sample inputs before compositing
- Use atomic groups
(?>...) or possessive quantifiers ++/*+` (if supported) to prevent backtracking exploits
- Score the regex for ReDoS risk:
npm i redoS → checkRegex(regex) should report low catastrophic backtracking risk
- Favor simpler alternatives: string methods (
split, match), String.prototype.replace, or parser combinators if the pattern exceeds ~20 characters or has nested quantifiers
- Document the final regex with inline comments
/** @type {RegExp} */ and a brief explanation of each section
// Safe regex: extract filename without extension from a path
// Breakdown: ^ anchors start, [^/]+ matches one or more non-slash chars, \. matches literal dot, $ anchors end
// Atomic group prevents backtracking on malicious inputs
const safeFilenameRegex = /^(?>[^/]+)\.([^.]+)$/;
// Test cases
const tests = [
{ input: '/path/to/document.pdf', expected: 'document', desc: 'pdf extension' },
{ input: 'archive.tar.gz', expected: 'archive', desc: 'double extension (should match first)' },
{ input: 'noext', expected: 'noext', desc: 'no dot, return basename' },
];
tests.forEach(({ input, expected, desc }) => {
const match = input.match(safeFilenameRegex);
const ok = match && match[1] === expected;
console.log(`${desc}: "${input}" → ${match ? match[1] : 'null'} ${ok ? '✅' : '❌'}`);
});
# ReDoS safety check (Node)
const { checkRegex } = require('redoS');
const regex = /^(?>[^/]+)\.([^.]+)$/;
const result = checkRegex(regex, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.');
console.log(result); // { safe: true, maxIterations: ... } or warns about backtracking
1---2name: regex-builder-parser3description: Construct ReDoS-safe regular expressions and string parser logic for complex input string extraction.4---56# Regex Builder & Parser78## Prerequisites & Dependencies9- Node.js 18+ or Python 3.10+ with regex engine knowledge10- `npm i redoS` (for safety scoring) or Python's `re` module + `rstr` for generation11- Comfortable with character classes, quantifiers, grouping, and anchors1213## Execution Steps141. Clearly define the input format and extraction goal: what substrings, delimiters, or patterns must be captured152. Sketch the regex on paper first: anchors (`^`, `$`), delimiters, optional groups, alternation (`|`)163. Build the regex incrementally, testing each component against sample inputs before compositing174. Use atomic groups `(?>...)` or possessive quantifiers `++`/`*`+` (if supported) to prevent backtracking exploits185. Score the regex for ReDoS risk: `npm i redoS` → `checkRegex(regex)` should report low catastrophic backtracking risk196. Favor simpler alternatives: string methods (`split`, `match`), `String.prototype.replace`, or parser combinators if the pattern exceeds ~20 characters or has nested quantifiers207. Document the final regex with inline comments `/** @type {RegExp} */` and a brief explanation of each section2122```javascript23// Safe regex: extract filename without extension from a path24// Breakdown: ^ anchors start, [^/]+ matches one or more non-slash chars, \. matches literal dot, $ anchors end25// Atomic group prevents backtracking on malicious inputs26const safeFilenameRegex = /^(?>[^/]+)\.([^.]+)$/;2728// Test cases29const tests = [30 { input: '/path/to/document.pdf', expected: 'document', desc: 'pdf extension' },31 { input: 'archive.tar.gz', expected: 'archive', desc: 'double extension (should match first)' },32 { input: 'noext', expected: 'noext', desc: 'no dot, return basename' },33];3435tests.forEach(({ input, expected, desc }) => {36 const match = input.match(safeFilenameRegex);37 const ok = match && match[1] === expected;38 console.log(`${desc}: "${input}" → ${match ? match[1] : 'null'} ${ok ? '✅' : '❌'}`);39});40```4142```bash43# ReDoS safety check (Node)44const { checkRegex } = require('redoS');45const regex = /^(?>[^/]+)\.([^.]+)$/;46const result = checkRegex(regex, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.');47console.log(result); // { safe: true, maxIterations: ... } or warns about backtracking48```49```