Javascript nbsp从excel工作表复制时添加到字符串

Javascript nbsp从excel工作表复制时添加到字符串,javascript,Javascript,当我将带有多个不间断空格的字符串从我的邮件复制到excel工作表,然后复制到我的站点中的a文本框时,会向其中添加一个nbsp,这会在将其解析为xml时出错。如何使用javascript进行检查?在脚本开头添加此项 String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,""); } 像这样使用它 var theString = ' a string '; var theStringWithNoSpaces

当我将带有多个不间断空格的字符串从我的邮件复制到excel工作表,然后复制到我的站点中的a文本框时,会向其中添加一个nbsp,这会在将其解析为xml时出错。如何使用javascript进行检查?

在脚本开头添加此项

String.prototype.trim = function()
{
   return this.replace(/^\s+|\s+$/g,"");
}
像这样使用它

var theString = ' a string ';
var theStringWithNoSpaces = theString.trim(); 
//Removes the spaces at the start and end, but not the space in the middle of 'a' and 'string'
如果您使用的是jquery,那么该方法是内置的。简单使用

var theString = ' a string ';
var theStringWithNoSpaces = $.trim(theString);

请参阅此链接:可能是usefull@manish,据我所知,OP希望清除变量中的多余空间。除非我弄错了,他真的想检查一下角色