A simple JavaScript method that removes all new lines and multiple spaces (including tab spaces) by a single space
Overview
Regular expressions are those I don't use so frequently so I need to wrap them all in a method with a short explanation about what they do.
So I created a simple JavaScript method that removes all newlines and multiple spaces (including tab spaces) by a single space
The code
/**
* It replace all new lines and multiple spaces (including tab spaces) by a single space
* @param {string } text
* @return {string} a new cleaned string
*/
function cleanUpSpaces(text) {
if (!text) {
return text;
}
// \s{2,} matches any white space (length >= 2) character (equal to [\r\n\t\f\v ])
return text.replace(/\s{2,}/g, ' ');
};
Output example
// given
`SELECT *
FROM account WHERE id = 1234`
// output
SELECT * FROM account WHERE id = 1234