In fact, the problem is: if your js itself is unicode encoded, then you can use regular expression s to remove all white spaces, but if your js is utf-8 encoded, then the regular expression cannot handle spaces encoded as 160.
Below I first use a regular expression to remove the spaces coded as 32, and then use a recursive method to remove the unicode spaces on both sides of the string.
/**Remove spaces from both ends of characters Start
*@author Ao Shiwei
*@version v1.0
*@date 2009/11/14 22:51
*/
String .prototype.trim = function() {
var r = this.replace(/(^s*)|(s*$)/g, "");
r = Lremoveblank(r);
r = Rremoveblank(r);
return r;
}
function Lremoveblank(s) {
if (s.length == 1 && s.charCodeAt(0) == 160)
return "";
if (s.charCodeAt(0) == 160) {
s = s.substr(1, s.length - 1);
return removeblank(s );
}
else {
return s;
}
}
function Rremoveblank(s) {
if (s.length == 1 && s .charCodeAt(0) == 160)
return "";
if (s.charCodeAt(s.length-1) == 160) {
s = s.substr(0, s.length - 1);
return Rremoveblank(s);
}
else {
return s;
}
}
//-Remove both ends of characters Ending with spaces
//e.g.
var a = " a ";
alert("b" a.trim() "b");