当前位置: 代码迷 >> JavaScript >> JS去掉空格有关问题
  详细解决方案

JS去掉空格有关问题

热度:287   发布时间:2012-03-04 11:13:33.0
JS去掉空格问题
原代码是:
function   getlengthB(str)
{   return   str.replace(/[ "   "^\x00-\xff\]/g, " ").length;
}
function   Checkdata()
{
document.getElementById( "titlestr ").innerHTML= " ";
document.getElementById( "constr ").innerHTML= " ";
             
ischeck=true
                CheckUbbUse( 'UserName ',1,document.getElementById( 'Body ').value) //定员帖检查
if   (getlengthB(document.Dvform.topic.value) <3   &&   ispostnew==1)
{
document.getElementById( "titlestr ").innerHTML= "   <font   color=\ "#FF0000\ "> ←您忘记填写标题 </font> "
document.Dvform.topic.focus();
ischeck=false
}
想在getlengthB函数中增加去掉空格的命令

------解决方案--------------------
function getlengthB(str)
{
str = str.replace(/\s/g, " ");
return str.replace(/[ " "^\x00-\xff\]/g, " ").length;
}
------解决方案--------------------

function getlengthB(str)
{
return str.replace(/[ " "^\x00-\xff\]/g, " ").length;
}

String.prototype.trim = function() {
return this.replace(/(^\s*)|(\s*$)/g, " "); // 用正则表达式将前后空格
}
  相关解决方案