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.
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.
A quick 3-question quiz on this lesson — instant feedback.
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.
Project-based JavaScript Intermediate courses if you want a structured paid path after the free lessons. Wait for a sale — prices drop often.
Browse on Udemy →Search Amazon for JavaScript Intermediate books — check the publication year so the examples match current versions.
Search Amazon →