BeginnerCoder

Lesson 17 — Regular expressions

Match patterns in text

A regular expression (regex) describes a text pattern between slashes, like /cat/. Special symbols make it powerful: ^ means "start of the text", $ means "end", [0-9] means any one digit, and + means "one or more of the previous". .test(text) returns true or false.

const hasCat = /cat/;
console.log(hasCat.test("my cat")); // true
console.log(/^a/.test("banana"));   // false — does not start with a

Why it matters: checking postcodes, phone numbers and passwords in forms is done with regex.

Want it another way?
Task

Write a pattern that matches text made only of digits: "12345" → true, "12a" → false, "" → false.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with JavaScript Intermediate

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.