DEV Community

Discussion on: How to Trim String in JavaScript

Collapse
 
jalaj profile image
Jalaj β€’

The solution given in the Community Input removes all the whitespace from the string. That isn't trimming exactly. The correct way to do it should be...

let str = "      Samantha Ming      "
let trimmedStr = str.replace(/^\s+ | \s+$/g , '')    

console.log(trimmedStr)     // "Samantha Ming"
Collapse
 
samanthaming profile image
Samantha Ming β€’

OH good call! let me add your point to the code notes! Thanks for info πŸ‘