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();
精彩评论