freecoding.school100% FREE · NO SIGNUP
Closure CavesISSUE #31 of 61

regex · pattern matching

LambdaVSThe Mutator
Lambda saysRegular expressions match patterns in strings — for validation, search, and replace.

A regex like /\d+/ describes a pattern (here, one or more digits). Use str.match, test, replace, and split with them. Build literals with slashes or new RegExp(str) for dynamic patterns. Start simple — regex grows cryptic fast, so comment non-trivial ones.

Power-ups you unlock

The Mutator attacks — common mistakes

Boss battleTest whether a string contains digits, then extract them with match.

Example code

<!doctype html><html><head><meta charset="utf-8"></head>
<body style="background:#06040d;color:#e6e0ff;font-family:monospace;padding:20px"><pre id="o"></pre>
<script>const s='order 42 ships in 3 days'; const nums=s.match(/\d+/g);
document.getElementById('o').textContent='has digits: '+/\d/.test(s)+'\nnumbers: '+nums;</script></body></html>
▶ Open the interactive comic issue
‹ Math · Floor · Ceil · Round · RandomRegex · Flags · G · I · M · S · U · Y ›