xxxxxxxxxx
let text = " Hello World! "; // output : Hello World!
let result = text.trim();
//Remove spaces with replace() using a regular expression:
let text = " Hello World! ";
let result = text.replace(/^\s+|\s+$/gm,''); // output : Hello World!
xxxxxxxxxx
var str=" I have outer spaces ";
var cleanStr=str.trim();//trim() returns string with outer spaces removed
xxxxxxxxxx
const greeting = ' Hello world! ';
console.log(greeting);
// Expected output: " Hello world! ";
console.log(greeting.trim());
// Expected output: "Hello world!";
xxxxxxxxxx
// to change many spaces into only one use this:
string = string.replace(/\s\s+/g, ' ');
xxxxxxxxxx
var str=" I have outer spaces ";
var cleanStr=str.trim(); //return:"I have outer spaces" >> outer spaces removed
xxxxxxxxxx
const str = " Hello, World! ";
const trimmedStr = str.trim();
console.log(trimmedStr); // Output: "Hello, World!"
xxxxxxxxxx
var str = ' foo ';
str.trim(); //return string 'foo'
var str = 'foo ';
str.trim(); // return string 'foo'
xxxxxxxxxx
const string = " H ell o world "
const modified = string.trim()
console.log(string)
// H ell o world
console.log(modified)
// H ell o world