There are a lot of scripts out there to trim a string in JavaScript, but none that allow you to just left trim a string.
This is what I use to trim:
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,"");
}
I would like to change this a little and create a new function called leftTrim that only removes the leading space.
Use:
String.prototype.leftTrim = function() {
return this.replace(/^\s+/,"");
}
In the regex the:
so....
Note: The g
flag at the end of your regex is unnecessary as the anchors (^ and $) explicitly define what will match. There cannot be multiple matches.
See https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/RegExp for details on regex syntax in javascript