how to remove more than one space between two words of string in angular?

  • A+
Category:Languages

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() );

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: