Skip to main content

JavaScript String replace — exercises and bug fixes

5 hands-on exercises and 5 find-the-bug challenges for the JavaScript String replace 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

    Replace the first match

    easy10 min

    Change only the first cat to dog.

  2. 2

    Replace every match

    easy10 min

    Change all three cats to dogs.

  3. 3

    Remove characters

    easy10 min

    Strip the dashes out of the phone number.

  4. 4

    Swap a word

    easy10 min

    Replace the word world with everyone.

  5. 5

    Clean up spacing

    medium10 min

    Trim the text and collapse the triple space.

Find the bug(5)

  1. 1

    Fix only the first replaced

    easy8 min

    Every match should change, not just the first.

  2. 2

    Fix the result thrown away

    easy8 min

    replace returns a new string and leaves the original alone.

  3. 3

    Fix the case mismatch

    easy8 min

    The capitalised word does not match the lowercase search.

  4. 4

    Fix the replacement left in

    easy8 min

    The dashes should be removed, not turned into spaces.

  5. 5

    Fix the arguments swapped

    easy8 min

    The text to find comes first and the replacement second.

Ready to write the code?

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

Open the workspace