Trim JavaScript
var str=" I have outer spaces ";
var cleanStr=str.trim();//trim() returns string with outer spaces removed
Grepper
var str=" I have outer spaces ";
var cleanStr=str.trim();//trim() returns string with outer spaces removed
let text1 = " Hello World! ";
let text2 = text1.trim();
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!