JavaScript / ECMAScript

Trailing Whitespace (Per Line) in JS

Match trailing spaces and tabs at the end of each line — the regex linters use to flag dirty whitespace.

Try it in the JS tester →

Pattern

regexJS
[ \t]+$   (flags: gm)

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("[ \\t]+$", "gm");
const input = "good line\\nbad line  \\nfine";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));

Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).

How the pattern works

[ \t]+ matches one or more space or tab characters. $ with the m (multiline) flag anchors to the end of EACH line, not the whole string. The g flag iterates so a multi-line string surfaces every offending line. Pair with `replace(re, '')` to strip.

Examples

Input

good line\nbad line \nfine

Matches

Input

tab here\tand more\t\t

Matches

  • \t\t

Input

no trailing space

No match

Same pattern, other engines

← Back to Trailing Whitespace (Per Line) overview (all engines)