View Full Version : Need to replace part of a string

03-11-2004, 05:09 AM
I need to replace a string in a link as such:

Replace _e in "mypagexyz_e.html" with _f to produce "mypagexyz_f.html".

Note, I do not know the 'mypagexyz' part of the string, ie. it will change and I need one javascript to handle changing the _e to _f without knowing what precedes it in the string. The .html part will be constant. I also don't know if the string already has _e or _f. So, if _f replace with _e or if _e replace with _f.

New to javascript, read about string.replace and others, not sure how to write up the procedure.



03-11-2004, 05:42 AM
function myreplace(s){
if (/_e/.test(s)) s=s.replace(/_e/g,'_f');
else if (/_f/.test(s)) s=s.replace(/_f/g,'_e');
return s;
var str=myreplace("mypagexyz_e.html");
var str2=myreplace("mypagexyz_f.html");

The "g" part (global) means to replace all occurrences of the specified string.

03-11-2004, 05:52 AM
This does exactly what I need. Many thanks for your quick help!