Menu

gitpiper

compactWhitespace javascript Code Snippet in 2024

stringregexpbeginner

Last Updated: 20 April 2024

Compacts whitespaces in a string.

  • Use String.prototype.replace() with a regular expression to replace all occurrences of 2 or more whitespace characters with a single space.
const compactWhitespace = str => str.replace(/\s{2,}/g, ' ');
compactWhitespace('Lorem Ipsum'); // 'Lorem Ipsum' compactWhitespace('Lorem \n Ipsum'); // 'Lorem Ipsum'

javascript snippet similar to compactWhitespace For You in April 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️