![]() |
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=worldI would like the output to be: :
http%3A%2F%2Fdomain.com%2Ffolder%2Findex.php%3Fid%3D123&stuff%3Dbob%3B&hello%3Dworld |
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.) |
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=worldit should redirect me to :
http://www.thisisawebproxy.com/?url=http%3A%2F%2Fwww.mydomain.com%2Ffolder%2Findex.php%3Fid%3D123&stuff%3Dbob%3B&hello%3DworldThe 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. |
any ideas?
|
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? |
Re: regex replace all character A's with character B
ok I created a basic greasemonkey script:
:
var 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