Regular Expressions
A regular expression describes a text pattern. Write it between slashes, add flags such as g (all matches) and i (ignore case), and use test, match or replace.
Common pieces: \d is a digit, \w a word character, \s a space, + means one or more, * zero or more, ^ the start and $ the end.
Example
const phone = /^0\d{10}$/;
console.log(phone.test("08012345678"));
console.log(phone.test("12345"));
const text = "Order 12 and order 345";
console.log(text.match(/\d+/g));
console.log(text.replace(/order/gi, "item"));Exercise
Find all matches, not just the first.
const re = /cat/;
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.