Skip to main content

JavaScript Regex Groups — exercises and bug fixes

5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Regex Groups lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.

Exercises(5)

  1. 1

    Capture the year

    easy10 min

    Pull the year out of the date with a group.

  2. 2

    Use a named group

    easy10 min

    Name the group and read it from groups.

  3. 3

    Repeat a group

    easy10 min

    Check the text is the pair ab repeated.

  4. 4

    Match one of two words

    easy10 min

    Accept only cat or dog.

  5. 5

    Rearrange with groups

    medium10 min

    Turn the ISO date into the British order.

Find the bug(5)

  1. 1

    Fix the whole match used as a group

    easy8 min

    Position 0 is the full match; the groups start at 1.

  2. 2

    Fix the replacement references

    easy8 min

    The parts are in the wrong order for a British date.

  3. 3

    Fix the alternation without brackets

    easy8 min

    Without brackets the anchors apply to only one alternative.

  4. 4

    Fix the quantifier on one character

    easy8 min

    The quantifier should repeat the whole pair, not just the b.

  5. 5

    Fix the named group read by number

    easy8 min

    A named group is read through groups, by its name.

Ready to write the code?

The editor, the automatic checks and the worked solutions open up with an account.

Open the workspace