How do I trim a string in JavaScript? That is, how do I remove all whitespace from the beginning and the end of the string in JavaScript?
There are a lot of implementations that can be used. The most obvious seems to be something like this:
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); }; " foo bar ".trim(); // "foo bar"