FSM Full Stack Masterclass
Platform under construction
JavaScript course badge

Regular Expressions

Advanced

RegExp

RegExp objects describe text patterns. They are useful for validation, searching, extraction and controlled replacement.

const pattern = /javascript/i;

console.log(pattern.test("JavaScript basics"));

Regular Expressions

A regular expression is a compact language for matching text.

A regular expression describes a text pattern. JavaScript can use that pattern to test text, find matches, extract parts or replace text.

Regex syntax is powerful, but it can become unreadable quickly. Good regex code names the pattern, limits scope and explains non-obvious intent.

Use regex for pattern matching, not for parsing every possible structured language. HTML, JSON and URLs usually deserve proper parsers or dedicated APIs.

Literal syntax

/pattern/ creates a RegExp directly.

Constructor syntax

new RegExp(value) builds a pattern dynamically.

test

Returns true or false for a match.

Pattern scope

A small regex is usually better than one huge pattern.

Examples

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

Named pattern with clear boundaries

const slugPattern = /^[a-z0-9-]+$/;

function isSlug(value) {
  return slugPattern.test(value);
}

Unexplained pattern inline

if (/^[a-z0-9-]+$/.test(value)) {
  save(value);
}

// Works, but the intent is hidden.

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.

Literal regex

Use a literal for static patterns.

const pattern = /javascript/i;
pattern.test("JavaScript basics");

Constructor regex

Use the constructor for dynamic pattern text.

const word = "css";
const pattern = new RegExp(word, "i");

Simple validation

Use anchors to match the whole string.

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

Find a match

match returns details when a match exists.

const text = "Version 2026";
const match = text.match(/\d{4}/);
console.log(match?.[0]);

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.

Name important patterns

A variable name explains what the regex means.

Use anchors for full validation

^ and $ prevent partial matches.

Escape dynamic input

User-provided pattern text can change regex meaning.

Keep patterns small

Large regexes are hard to review.

Prefer parsers for structured formats

Do not parse JSON or HTML with one regex.

Test edge cases

Regex behavior changes quickly with small syntax changes.

Production thinking

A regex without tests is a guess with punctuation.

Why does this matter?

Regex can remove a lot of manual string code, but unreadable regex can also become a hidden bug factory.

Accessibility

Validation patterns should produce helpful messages, not just reject input silently.

Production note

Production regexes should be named, tested and limited to the problem they actually solve.

SEO note

Regex is useful for slug validation and URL cleanup, but canonical URL rules should be explicit.

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.

  • Allow underscores and inspect the pattern change.
  • Remove the anchors and test a partial match.
  • Create a new pattern with the RegExp constructor.

Practice assignment

Do this before moving to the next topic.

  1. Write one regex literal.
  2. Use test on a string.
  3. Add anchors for full-string validation.

Try it yourself

Test a slug pattern

Live preview

Self-check

Before you continue, prove that you understand RegExp.

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 what RegExp is?
  2. Can you explain literal versus constructor syntax?
  3. Can you explain why anchors matter?
  4. Can you explain why naming patterns helps?
  5. Can you explain when not to use regex?