Regexdrill

Level 4: Extraction and cleanup Exercise 38 of 52 Pro

Collapse repeated spaces

A regex exercise from Regexdrill. Solve it with a pattern box with live highlighting in your browser, checked against hidden test strings, and it is checked the moment you submit.

The task

Replace every run of two or more spaces with a single space.

What this exercise teaches

A space followed by {2,} (or a space and +) with the g flag turns every run into one space.

A hint if you get stuck

Match a space repeated two or more times, and use the g flag so every run changes.

This exercise is part of Pro

Pro unlocks all 52 exercises, with worked solutions and progress saved to your account. The free exercises work without an account.

You create a free account first, so Pro is attached to it.

Monthly is charged today and then every month until you cancel, in one click from your account page. Lifetime is one payment with no renewals. Full refund within 14 days of paying.

Start with the free exercises

More exercises in Extraction and cleanup