Multiline: trailing whitespace on every line
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
Each test is a block of several lines. Remove the spaces and tabs at the end of every line. Blank lines must stay as blank lines, leading whitespace stays, and nothing else changes. Use the m flag.
What this exercise teaches
[ \t]+$ with the gm flags removes trailing spaces and tabs on every line. \s+$ would swallow line breaks too, joining a blank line to the line above it.
A hint if you get stuck
A class of space and tab, repeated, then $ with the m flag. \s also matches line breaks, which causes a problem here.
Try it now, free, no account needed
Every exercise on Regexdrill is free to try. Open it on the practice page, solve it, and get instant feedback with a hint. A free account adds the worked solution; Pro adds timed mode and the certificate.
Open this exerciseMore exercises in Advanced
- Lookahead: amounts before USDFree
- Negative lookahead: not starting with unFree
- Lookbehind: amounts after $Free
- Negative lookbehind: positive numbers onlyFree
- Lazy vs greedy: the first quoted textFree
- Named groups: year, month, dayFree
- Password rules with lookaheadsFree
- Word boundaries: replace a whole wordFree
- Unicode letters with the u flagFree
- Multiline flag: TODO linesFree
- Backreference across lines: duplicate linesFree
- Named groups in a replacementFree
- Multiline anchors: lines ending in a semicolonFree
- Negative lookahead: numbers that are not percentagesFree