Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 10th, 2007, 5:10 PM   #1
atomicrabbit
Newbie
 
Join Date: Sep 2007
Posts: 13
Rep Power: 0 atomicrabbit is on a distinguished road
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
atomicrabbit is offline   Reply With Quote
Old Oct 10th, 2007, 6:34 PM   #2
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 843
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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.
titaniumdecoy is offline   Reply With Quote
Old Oct 10th, 2007, 11:50 PM   #3
atomicrabbit
Newbie
 
Join Date: Sep 2007
Posts: 13
Rep Power: 0 atomicrabbit is on a distinguished road
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 is offline   Reply With Quote
Old Oct 12th, 2007, 12:23 PM   #4
atomicrabbit
Newbie
 
Join Date: Sep 2007
Posts: 13
Rep Power: 0 atomicrabbit is on a distinguished road
any ideas?
atomicrabbit is offline   Reply With Quote
Old Oct 17th, 2007, 12:14 PM   #5
atomicrabbit
Newbie
 
Join Date: Sep 2007
Posts: 13
Rep Power: 0 atomicrabbit is on a distinguished road
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 is offline   Reply With Quote
Old Oct 17th, 2007, 1:07 PM   #6
atomicrabbit
Newbie
 
Join Date: Sep 2007
Posts: 13
Rep Power: 0 atomicrabbit is on a distinguished road
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;
atomicrabbit is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:29 PM.

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