test
Boolean answer from a RegExp.
JavaScript connects regex to strings through methods such as test, exec, match, matchAll, replace, split and search.
const text = "HTML CSS JavaScript"; const matches = text.match(/[A-Z]+/g); console.log(matches);
Regular Expressions
Use test when you only need true or false. Use match when you need match data from one string.
Use matchAll with a global regex when you need every match with capture groups. Use replace for controlled transformation.
The method choice matters. Code is easier to read when the method says whether you are checking, extracting, replacing or splitting.
Boolean answer from a RegExp.
Match details from a RegExp object.
String methods for match data.
Transform or divide text using a pattern.
Examples
const slugPattern = /^[a-z0-9-]+$/; const isSlug = slugPattern.test(value); const words = text.match(/\b\w+\b/g) ?? [];
const match = value.match(/^[a-z0-9-]+$/);
if (match) {
save(value);
}
// test communicates the boolean intent better.
Code patterns
These examples are intentionally small. Regex becomes useful when each pattern has one clear job and a readable method around it.
Use for validation checks.
const isValid = /^[a-z0-9-]+$/.test("array-methods");
Find matches from a string.
const matches = "HTML CSS".match(/[A-Z]+/g);
Extract all named groups.
const text = "score:42 level:7"; const pattern = /(?<name>[a-z]+):(?<value>\d+)/g; const pairs = [...text.matchAll(pattern)];
Transform matches with code.
const text = "html css"; const result = text.replace(/\b[a-z]/g, (letter) => letter.toUpperCase());
Rules that matter
Regular expressions are compact by design. Good JavaScript around the pattern is what makes them safe to maintain.
It communicates validation intent.
It belongs to the string being searched.
It works well with global capture groups.
A function keeps replacement logic clear.
Regex separators can handle repeated whitespace or punctuation.
match can return null when there is no match.
Production thinking
The method is part of readability. A good method choice tells readers what the regex is doing before they decode the pattern.
Search, replacement and validation should produce clear messages and avoid destroying meaningful text.
Production regex methods should handle no-match cases and avoid stateful global surprises.
Regex replacement can normalize URLs and headings, but transformations should be deterministic.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
Regex skill is not memorizing every token. It is knowing how to build small patterns and choose the right string method around them.
Chapter checkpoint
Finish this chapter by turning the lessons into a small practical proof.
Build a small example that combines three lessons from this chapter.
Can you explain the important tradeoff without reading from the page?