I have the following jquery statement. I wish to remove the whitespace as shown below. So if I have a word like:
For example
#Operating/SystemI would like the
end result to show me
#Operating\/System. (ie with a
escape sequence).
- But if I have
#Operating/System testthen I want to show
#Operating\/System+ escape
sequence for space. The.replace(/ /,'')
part is incorrect but.replace("/","\\/")works
well as per my requirements.
Please help!
$("#word" + lbl.eq(i).text().replace("/","\\/").replace(/ /,'')).hide();
This matches all spaces and slashes in a string (and saves the respective char in group
$1):replacement with
means a backslash plus the original char in group
$1.Side advantage – there is only a singe call to
replace().EDIT: As requested by the OP, a short explanation of the regular expression
/([ /])/g. It breaks down as follows:/ # start of regex literal ( # start of match group $1 [ /] # a character class (spaces and slashes) ) # end of group $1 /g # end of regex literal + "global" modifierWhen used with
replace()as above, all spaces and slashes are replaced with themselves, preceded by a backslash.