JavaScript Academy · Lesson

Regex basics — literals, test, anchors, and flags

Create simple regexes, test text, use anchors (^ $), and apply common flags (i, g) for small parsing tasks.

Lesson 1 of 38 steps

Regex basics — literals, test, anchors, and flags is a free JavaScript Academy lesson on CoddyKit. This is lesson 1 of 3. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 3 lessons in total.

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)
Regex basics — literals, test, anchors, and flags — illustration 1

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"));
Regex basics — literals, test, anchors, and flags — illustration 2

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);
Regex basics — literals, test, anchors, and flags — illustration 3

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);
Regex basics — literals, test, anchors, and flags — illustration 4

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);
Regex basics — literals, test, anchors, and flags — illustration 5

Escape essentials

Escape tips:

  • Specials: . * + ? ( ) [ ] { } | ^ $
  • To match a dot, use \\.
  • Keep patterns small and readable for beginners.
Regex basics — literals, test, anchors, and flags — illustration 6

Flags basics quiz

Quick check: Regex flags.

Regex basics — literals, test, anchors, and flags — illustration 7

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.

Regex basics — literals, test, anchors, and flags — illustration 8
Free to start

Learn JavaScript with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
59
Lessons
200

Frequently Asked Questions

Is the “Regex basics — literals, test, anchors, and flags” lesson free?

Yes — any 3 lessons of the JavaScript Academy learning path, including “Regex basics — literals, test, anchors, and flags”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 3 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 3.

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

  1. Regex basics — literals, test, anchors, and flags
  2. Groups, Captures, and Replace
  3. Validation & small pitfalls
← Back to JavaScript Academy