2012-11-20 73 views
0

如何使用javascript从字符串中删除所有空格。 我有这样this--使用javascript删除空格

var Title = ""; 
var Str="g g"; 
Title = Str.replace(/^\s+|\s+$/g, ''); 
this gives me result Title=g g 
how to get result Title=gg 

回答

2

使用正则表达式如下代码:

Str.replace(/\s+/g, ''); 

你现在所拥有的该一个刚刚从strippes开始或您的字符串的结尾空格。

+0

其工作的感谢 –

0

试试这个

Title = Str.replace(/ /g, ''); 
0

更改代码:

var title = Str.replace(/\s+/g, ''); 

你原来的正则表达式将只从字符串开始处或结束修整空间。

0

你可以使用:

var Str="g g"; 
Str.replace(" ", "");