user176820
user176820

Reputation:

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!

$("#word" + lbl.eq(i).text().replace("/","\\/").replace(/ /,'')).hide();

Upvotes: 0

Views: 1028

Answers (2)

Roatin Marth
Roatin Marth

Reputation: 24115

Try the global g modifier:

.replace(/ /g, '')

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

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

Upvotes: 3

Josh Stodola
Josh Stodola

Reputation: 82533

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();

Upvotes: 2

Related Questions