Drill

ProblemsTypeScript › text

Tidy up pasted text

easytextTypeScript

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: string): string {
  
}

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 TypeScript
function normaliseSpace(text: string): string {
  return text.split(/\s+/).filter((w) => w.length > 0).join(' ');
}

The same problem in another language

More text problems in TypeScript