Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Mar 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need to replace part of a string

    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.

    Thanks!

    -Mike

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,074
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Code:
    function myreplace(s){
      alert('before:'+s);
      if (/_e/.test(s)) s=s.replace(/_e/g,'_f');
      else if (/_f/.test(s)) s=s.replace(/_f/g,'_e');
      alert('after:'+s);
      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.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    New to the CF scene
    Join Date
    Mar 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    This works, thanks!

    This does exactly what I need. Many thanks for your quick help!

    -Mike


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •