Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Other Programming Languages (http://www.programmingforums.org/forum38.html)
-   -   regex replace all character A's with character B (http://www.programmingforums.org/showthread.php?t=14154)

atomicrabbit Oct 10th, 2007 6:10 PM

regex replace all character A's with character B
 
Simply put... how can I replace all of a specific character in a string with another character using regex?

I'm using it to replace all the /?&=:; in a URL with their respective hex value. So if I have a URL like this:
:

http://domain.com/folder/index.php?id=123&stuff=bob;&hello=world

I would like the output to be:
:

http%3A%2F%2Fdomain.com%2Ffolder%2Findex.php%3Fid%3D123&stuff%3Dbob%3B&hello%3Dworld

titaniumdecoy Oct 10th, 2007 7:34 PM

How you replace characters in a string depends on the language you are using.

For the application you describe, you would be better off using that language's string manipulation features. Regular expressions are unnecessary for such a simple task.

Some languages provide built-in features to convert a string to a URL-encoded string. In PHP, the urlencode and rawurlencode functions do just this.

(Also, the & is a legal character in URLs and does not need to be encoded as in your example string.)

atomicrabbit Oct 11th, 2007 12:50 AM

for THIS specific task, it NEEDS to be a regular expression. I am using a firefox extension called Redirector and the extension redirects URL based on rules the user sets using either wildcards or regex. Long story short, I need to replace the symbols I mentioned in my last post with their respective hex character.

btw, I realize the & is a legal character and so are all the other characters, but the url is being passed to a web proxy which cannot handle the actual symbols and require the hex values in order for it to work.

the main goal here is to be able to trap a specific URL and redirect it through a web proxy. So for example, if I type in:
:

http://www.mydomain.com/folder/index.php?id=123&stuff=bob;&hello=world

it should redirect me to
:

http://www.thisisawebproxy.com/?url=http%3A%2F%2Fwww.mydomain.com%2Ffolder%2Findex.php%3Fid%3D123&stuff%3Dbob%3B&hello%3Dworld

The firefox extension does the work of the first part (trapping the URL and concatenating the web proxy address to the beginning), I just need to know how to replace specific characters with something else using regular expressions.

atomicrabbit Oct 12th, 2007 1:23 PM

any ideas?

atomicrabbit Oct 17th, 2007 1:14 PM

Re: regex replace all character A's with character B
 
what happened to all the posts ??? did the server crash??

I think titaniumdecoy suggested using greasemonkey. What language does greasemonkey use?

atomicrabbit Oct 17th, 2007 2:07 PM

Re: regex replace all character A's with character B
 
ok I created a basic greasemonkey script:

:

var strNewUrl
var strOrigUrl
strOrigUrl = window.location.href;
strNewUrl = strOrigUrl.replace(/\%/g, '%25'); //must be first or else it will replace the hex values '%' character
strNewUrl = strNewUrl.replace(/\?/g, '%3f');
strNewUrl = strNewUrl.replace(/\&/g, '%26');
strNewUrl = strNewUrl.replace(/\=/g, '%3d');
strNewUrl = strNewUrl.replace(/\@/g, '%40');
strNewUrl = strNewUrl.replace(/\#/g, '%23');
strNewUrl = strNewUrl.replace(/\~/g, '%7e');
strNewUrl = strNewUrl.replace(/\_/g, '%5f');
strNewUrl = strNewUrl.replace(/\;/g, '%3b');
strNewUrl = strNewUrl.replace(/\,/g, '%2c');
strNewUrl = strNewUrl.replace(/\./g, '%2e');
strNewUrl = strNewUrl.replace(/\//g, '%2f');

window.location.href = "http://www.web-proxy-address-goes-here.com/whatever.php?u=" + strNewUrl;



All times are GMT -5. The time now is 3:20 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC