FSM Full Stack Masterclass
Platform under construction
JavaScript course badge

Regular Expressions

Advanced

RegExp Methods

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

Regex methods answer different questions about text.

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.

test

Boolean answer from a RegExp.

exec

Match details from a RegExp object.

match / matchAll

String methods for match data.

replace / split

Transform or divide text using a pattern.

Examples

Regex should be named, scoped and tested against edge cases.

Method matches the intent

const slugPattern = /^[a-z0-9-]+$/;
const isSlug = slugPattern.test(value);

const words = text.match(/\b\w+\b/g) ?? [];

Using extraction when only a boolean is needed

const match = value.match(/^[a-z0-9-]+$/);

if (match) {
  save(value);
}

// test communicates the boolean intent better.

Code patterns

Reusable examples for quick reference.

These examples are intentionally small. Regex becomes useful when each pattern has one clear job and a readable method around it.

test

Use for validation checks.

const isValid = /^[a-z0-9-]+$/.test("array-methods");

match

Find matches from a string.

const matches = "HTML CSS".match(/[A-Z]+/g);

matchAll

Extract all named groups.

const text = "score:42 level:7";
const pattern = /(?<name>[a-z]+):(?<value>\d+)/g;
const pairs = [...text.matchAll(pattern)];

replace callback

Transform matches with code.

const text = "html css";
const result = text.replace(/\b[a-z]/g, (letter) => letter.toUpperCase());

Rules that matter

Make the pattern purpose visible outside the pattern.

Regular expressions are compact by design. Good JavaScript around the pattern is what makes them safe to maintain.

Use test for true or false

It communicates validation intent.

Use match for simple extraction

It belongs to the string being searched.

Use matchAll for all captured matches

It works well with global capture groups.

Use replace callbacks for transformations

A function keeps replacement logic clear.

Use split for separators

Regex separators can handle repeated whitespace or punctuation.

Handle null results

match can return null when there is no match.

Production thinking

A regex without tests is a guess with punctuation.

Why does this matter?

The method is part of readability. A good method choice tells readers what the regex is doing before they decode the pattern.

Accessibility

Search, replacement and validation should produce clear messages and avoid destroying meaningful text.

Production note

Production regex methods should handle no-match cases and avoid stateful global surprises.

SEO note

Regex replacement can normalize URLs and headings, but transformations should be deterministic.

Live code lab

Change the HTML, CSS or JavaScript and run the result.

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

Try this now.

  • Replace matchAll with match and compare the output.
  • Add another pair to the textarea.
  • Use replace to wrap each number in brackets.

Practice assignment

Do this before moving to the next topic.

  1. Use test for validation.
  2. Use match for extraction.
  3. Use replace with a callback.

Try it yourself

Use matchAll and replace

Live preview

Self-check

Before you continue, prove that you understand RegExp Methods.

Advanced

Regex skill is not memorizing every token. It is knowing how to build small patterns and choose the right string method around them.

  1. Can you explain test?
  2. Can you explain match?
  3. Can you explain matchAll?
  4. Can you explain replace callbacks?
  5. Can you explain why null handling matters?

Chapter checkpoint

Regular Expressions checkpoint

Finish this chapter by turning the lessons into a small practical proof.

Build

Build a small example that combines three lessons from this chapter.

Deliverables

  • working code
  • short explanation
  • self-check answers

Quality checks

  • readable code
  • clear failure path
  • accessibility considered

Review question

Can you explain the important tradeoff without reading from the page?