Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with replace function 1

Status
Not open for further replies.

JeanPhil

Programmer
Dec 23, 2003
27
CA
Hi everyone. I use a javascript replace function to modify certain characters that I send in the URL. It work fine for most of them but when it comes to the + sign, I can only replace it once. Here is a sample of the replace I used :

string.replace(/%/gi,'%25').replace(/&/gi,'%26').replace(/#/gi,'%23').replace('+','%2B');

If I try to change the replace for the + sign to replace(/+/gi,'%2B') so that it replace all the signs I get an Unexpected quantifier error.

Is there something special about the + sign that I don't know ? It work for all the others but this one so I'm a little confused.

Any help would be greatly appreciated.

 
JeanPhil, it seems that you're trying to modify url strings that contain special characters. Is it possible to format the string with the unescape and escape functions instead?

string = escape(string);
string = unescape(string);

-kaht

banghead.gif
 
Thanks for the quick reply Kaht ...

No I have the same problem. It work for most character but not for the + sign !! Really weird.
 
AFAIK escape() won't touch '+' character.

About regular expressions: the following characters:

$ ( ) * + . [ ] ? \ | ^ { }

are part of regexp syntax and have special purpose. To declare them as data, use \ as escape character (\+ instead of +).
 
Thank you so much vongrunt !!! Now I only have to replace my function in about 200 pages :s !!
 
Ouch.. why not put that code in a JS file instead of hardcoding each page?
Code:
<script src="myJSfile.js"></script>
At least that way when you make changes, all of your pages will be reflected.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top