开发者

How do I strip white space when grabbing a text()

I have the following jquery statement. I wish to remove the whitespace as shown below. So if I have a word like:

For example

"#wordOperating System/test" I would like the end result to show me "#wordOperatingSystemtest". (ie without slash and space). The slash works fine but I can't get the space to be removed. Please help!

$("#w开发者_JS百科ord" + lbl.eq(i).text().replace("/","\\/").replace(/ /,'')).hide();


Try the global g modifier:

.replace(/ /g, '')

Same goes for your slash replacement (in case there are multiple /s in your string):

.replace(/\//g, '\\/')


You can do it with one simple regex...

var x = "wordOperating System/test";
x = x.replace(/\s|\//g, '');
alert(x);

So your code will be...

$("word" + lbl.eq(i).text().replace(/\s|\//g, '')).hide();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜