this.folderObj.folderName
is string and I print console
of that string aaa aaa
, I use trim()
but it removes only before and after white spaces,how to remove more than one space between two words of string and want output like this aaa aaa
folderObj : Folder = new Folder();
console.log(this.folderObj.folderName.trim()); // aaa aaa
expected console that i want
console.log(this.folderObj.folderName.trim()) // aaa aaa
Use a regular expression to match two or more space characters, and replace with a single space:
const folderName = ' aaa aaa';
console.log(
folderName
.replace(/ {2,}/g, ' ')
.trim()
);
Of course, you could also match one or more space characters instead with +
, the code will look nicer, but it'll be very slightly less efficient:
const folderName = ' aaa aaa';
console.log(
folderName
.replace(/ +/g, ' ')
.trim()
);
Try with replace()
to replace all single and multiple spaces with single space.
this.folderObj.folderName.trim().replace(/ +/g, ' ');
Demo:
console.log('aaa aaa'.replace(/ +/, ' '))
let folderName = ' abcd xya ';
console.log(folderName.replace(/\s+/g, ' ').trim());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With