I haven’t been able to find an example that does something similar to what I want, that is, replace:
% with %25
& with %26
/ with %2F
# with %23
” with space
\ with space
For the reference I’m using GWT with String.replaceAll, but I’m asking about Javascript since that is what it gets translated to anyway. I know about (component) URI encoding, but it’s not what I’m after, I only need these characters.
Later edit: I’m looking for a way to do it in one or two regexes, right now it’s done as this:
splits[i].replaceAll("%", "%25").replaceAll("&", "%26").replaceAll("/","%2F").replaceAll("#", "%23").replaceAll("\"", "").replaceAll("\\\\", " ");
but this seems ugly to me.
(This is for JavaScript, which is the language you asked about. Note that it will be fairly different in Java.)
In JavaScript, this is trivial using
replacewith a regular expression and passing in a function:Live example | source
If you don’t mind scanning the string twice rather than once, it can look a bit simpler (though not enough that I think it’s worth it):
…because we can do the
"and\replacement separately and get rid of the check for what we matched. Live example | source