<script language=javascript>
var newstr,tmp;
var str=" as ef gas dfsaf d d ";
alert(str);
tmp=/\s+/g;
newstr=str.replace(tmp," ");
alert(newstr);
</script>
下面的代码是临时想的,没有调试过,请各位看看是否可以。
Dim mystr ' as 要处理的字符串
Dim pos ' as 当前的字符的位置
Dim maxlen ' as 字符的长度
Dim tmpstr 'as 用来取得要去掉的空格
mystr=" as ef gas dfsaf d d "
tmpstr=""
'处理开头的空格
maxlen=len(mystr)
for i=1 to maxlen
if mid(mystr,i,1)<>" " then
pos=i'记录处理的位置,i-1就是开头的空格的长度
exit for
end if
next
mystr=right(mystr,maxlen-pos+1) '字符开头不要空格
'处理中间的空格,处理完成后,字符的结尾还有一个空格
maxlen=len(mystr)
for i=1 to maxlen
if mid(mystr,i,1)=" " then
tmpstr=tmpstr & " "
else
replace(mystr,tmpstr," ")
tmp=""
end if
next
'处理最后的一个空格
mystr=left(mystr,len(mystr)-1)
Function ReplaceTest(patrn, replStr)
Dim regEx, str1 ' 建立变量。
str1 = "The quick brown fox jumped over the lazy dog."
Set regEx = New RegExp ' 建立正则表达式。
regEx.Pattern = patrn ' 设置模式。
regEx.IgnoreCase = True ' 设置是否区分大小写。
ReplaceTest = regEx.Replace(str1, replStr) ' 作替换。
End Function