In this tutorial we are going to explain how to remove spaces using trim and without trim. When we don't use trim function we can do left or right trim on the string.
Removing spaces using trim
var text=" How to remove spaces on this string "; var trimmedText = text.trim(); console.log(text); console.log(trimmedText);
Removeing spaces without trim
You can use replace with regex to remove spaces like the following example:
var text=" How to remove spaces on this string "; trimmedText = str.replace(/^s+|s+$/gm,'');
var text=" How to remove spaces on this string "; ltrimmedText = str.replace(/^s+/,'');
var text=" How to remove spaces on this string "; rTrimmedText = str.replace(/s+$/,'');