Drill

ProblemsJavaScript › text

Tidy up pasted text

easytextJavaScript

Text pasted from a spreadsheet arrives with tabs, newlines and runs of spaces in it. The search box wants one clean line.

normaliseSpace(text: string) → string

Solve it in the editor →

Where you start

function normaliseSpace(text) {
  
}

Worked examples

CallResult
normaliseSpace(" hello world ")"hello world"
normaliseSpace("a\tb\nc")"a b c"
normaliseSpace("already clean")"already clean"
normaliseSpace(" ")""

Hint

Splitting on whitespace and joining with a single space does both jobs at once.

Reference solution in JavaScript
function normaliseSpace(text) {
  return text.split(/\s+/).filter((w) => w.length > 0).join(' ');
}

The same problem in another language

More text problems in JavaScript