Regex basics — literals, test, anchors, and flags
Create simple regexes, test text, use anchors (^ $), and apply common flags (i, g) for small parsing tasks.
Regex basics — literals, test, anchors, and flags is a free JavaScript Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Intro to regex
Goal: Recognize and apply tiny regex patterns.
- Create a regex literal like /cat/
- Check with test()
- Use anchors ^ and $
- Add flags: i (ignore case), g (global)

Literal + test()
Create a literal like /cat/ and call test(text) to get true/false.
// Use a regex literal and test() to check presence
const r = /cat/;
console.log("has cat?", r.test("my cat"));
console.log("has cat?", r.test("dog"));

Classes & match()
Common classes: \\d digit, \\w word char, \\s whitespace. Use g to get all matches.
// Character classes: \\d (digit), \\w (word), \\s (space)
const msg = "Order #245, table 7";
const numbers = msg.match(/\\d+/g);
console.log("numbers:", numbers);

Anchors
^ matches start; $ matches end. Together they can match a whole-string pattern.
// Anchors: ^ start, $ end
const a = /^hello/.test("hello world");
const b = /world$/.test("say world");
const c = /^hello$/.test("hello");
console.log("starts hello?", a, "ends world?", b, "exact hello?", c);

Flags overview
i ignores case; g finds all matches instead of only the first.
// Flags: i = ignore case, g = global (find all)
const text = "Red, red, RED";
const first = text.match(/red/i);
const all = text.match(/red/gi);
console.log("first:", first[0]);
console.log("all:", all);

Escape essentials
Escape tips:
- Specials: . * + ? ( ) [ ] { } | ^ $
- To match a dot, use \\.
- Keep patterns small and readable for beginners.

Flags basics quiz
Quick check: Regex flags.

Recap
Recap: Build a regex with a literal, check with test(), use ^/$ for start/end, and add i/g flags for case-insensitive and multi-match finds.

Frequently asked questions
Is the “Regex basics — literals, test, anchors, and flags” lesson free?
Yes — the full text of “Regex basics — literals, test, anchors, and flags” is free to read here on the web, and the JavaScript Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Regex basics — literals, test, anchors, and flags”?
Create simple regexes, test text, use anchors (^ $), and apply common flags (i, g) for small parsing tasks. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start JavaScript Academy?
No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Regex basics — literals, test, anchors, and flags” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this JavaScript Academy lesson?
Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Regex basics — literals, test, anchors, and flags
- Groups, Captures, and Replace
- Validation & small pitfalls