Hans Vn
Hans Vn

Reputation: 844

RegExp in actionscript 3: convert url to char coded text

I have a little AS3 script to convert all special characters in a string to their character codes.

Here is the script:

url = url.replace(new RegExp("%","g"),"%25")
            .replace(new RegExp("?","g"),"%3F")
            .replace(new RegExp(":","g"),"%3A")
            .replace(new RegExp("/","g"),"%2F")
            .replace(new RegExp("=","g"),"%3D")
            .replace(new RegExp("&","g"),"%26");

Now, I'm not even a beginner with RegExp, but I gave it a try. The little script seems to do the trick quite well, but only the question mark (?) isn't replaced.

Anyone who can tell me why?

If you can also tell me a more short way to code this, feel free to share it, I know this isn't the best practice of RegExp...

greets

Upvotes: 1

Views: 549

Answers (2)

crooksy88
crooksy88

Reputation: 3851

Not sure if this is of help but do you know about escape?

e.g.

var encodedURL:String = escape(url);

Upvotes: 2

You have to escape the question mark: "\?"

Upvotes: 3

Related Questions