I making some server reguests and the server reply me with a string that has a lot of spaces in front of the string. the string is USERNAME EXIST
I know how to use this:
String.prototype.killWhiteSpace = function() {
return this.replace(/\s/g, '');};
String.prototype.reduceWhiteSpace = function() {
return this.replace(/\s+/g, ' ');};
but the first the first answer me USERNAMEEXISTS and the second on ” USERNAME EXIST”(with one space in front of the string).
Is there any way to kill all white spaces before and after the string?
Use
^to match the start of a string and$to match the end of it in a regular expression:Normally stripping whitespace is called trimming and is already implemented natively in modern browsers. So you may want to use this:
Which will create a shim for trim if it doesn’t already exist, otherwise it will leave the native implementation (which is much faster) in place.