![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Sep 2007
Posts: 13
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Expert Programmer
|
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.) Last edited by titaniumdecoy; Oct 10th, 2007 at 6:48 PM. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Sep 2007
Posts: 13
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Sep 2007
Posts: 13
Rep Power: 0
![]() |
any ideas?
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: Sep 2007
Posts: 13
Rep Power: 0
![]() |
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? |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Sep 2007
Posts: 13
Rep Power: 0
![]() |
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; |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Regex Replace | kruptof | C# | 5 | Jul 30th, 2007 12:47 AM |
| Reading character input into an array (raw mode) | shoeyfighter | C | 3 | Nov 2nd, 2006 3:49 PM |
| Illegal character come out | weilee39 | Other Web Development Languages | 4 | Aug 2nd, 2006 9:50 PM |
| incrementing character array elements value | n00b | C++ | 7 | Jun 24th, 2006 3:44 AM |
| regex (preg_replace) parsing | para | PHP | 2 | Dec 31st, 2005 6:30 PM |