views:

110

answers:

2

Hi,

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();
+3  A: 

Try the global g modifier:

.replace(/ /g, '')

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

.replace(/\//g, '\\/')
Roatin Marth
-1 It is silly to use two separate expressions for this...
Josh Stodola
@Josh: consider the OP's own code sample was replacing `/` with `\/`, which this answer addresses.
Roatin Marth
+2  A: 

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();
Josh Stodola
okay. May be i am unclear (i am very new to jquery) but what I want is to have an escape char for space. So if I read text= Operating/System it replace that to Operating\/System when I run the code. In the same way I want to have an esc char for space. So that when it reads a space it adds and escape char for space in the same way. How would I do that. Please help.
SA
@SA: now you're saying you want to escape spaces? Which is it: remove spaces, remove slashes, *escape* slashes, or *escape* spaces?
Crescent Fresh
Yes. I ran the code and realized the issue. So when I read a space or s slash in the string i want to escape slashes and escape 1 space in the string.
SA