Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

(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 replace with a regular expression and passing in a function:

var str = "testing % & / # \" \\";
var result = str.replace(/[%&\/#"\\]/g, function(m) {
    return (m === '"' || m === '\\') ? " " : "%" + m.charCodeAt(0).toString(16);
});

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):

var str = "testing % & / # \" \\";
var result = str.replace(/["\\]/g, " ").replace(/[%&\/#]/g, function(m) {
    return "%" + m.charCodeAt(0).toString(16);
});

...because we can do the " and \ replacement separately and get rid of the check for what we matched. Live example | source

share|improve this answer
 
+1 I did not know you could do that!!! –  El Ronnoco Apr 24 '12 at 10:28
 
Great answer, I will wait a bit and mark it as the solution; even though it's not really doable in my app it solves the question elegantly. I was looking to see if there was a way to do it with a generic/standard regular expression with capture->replace. –  brainwash Apr 24 '12 at 12:39
 
@brainwash: "I was looking to see if there was a way to do it with a generic/standard regular expression with capture->replace..." Not in JavaScript; you can move things around using capture groups and placeholders (for instance, "testing".replace(/(t)(.)/g, "$2$1") => "etsitng"), but to do transformations, you have to use a function. I'm not familiar with any regex engine that would let you convert characters to their character codes without having it call back to a function you write (but then most of my experience with regex is in JavaScript) or, of course, doing them individually. –  T.J. Crowder Apr 24 '12 at 12:44
add comment
String.replace(/%/, '%25').replace(/&/, '%26').replace(/\//, '%2F').replace(/#/, '%23').replace(/"/, ' ').replace(/\\/, ' ');
share|improve this answer
 
That's exactly what I do now, but I was looking if there was a way to do it in one or maybe two steps. –  brainwash Apr 24 '12 at 10:57
1  
Several problems with the above: 1. There is no String.replace function, replace is a function on string instances (inherited from String.prototype), not the String constructor function. 2. This scans the string repeatedly. 3. This will only replace the first occurrence of each character, not all of them. –  T.J. Crowder Apr 24 '12 at 11:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.