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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Posts
    405
    Thanks
    17
    Thanked 2 Times in 2 Posts

    condense multiple url.replace

    I'm just trying to figure out how to simplify this repetitive set of possible url's to replace:

    Code:
    var url = (document[picName].src);
    	   
    	   // these all were to replace any of the possible choices if previously made. need to condense
    
    	   
    url = url.replace("_transparent", choice);
    url = url.replace("_auriculate", choice);
    url = url.replace("_cordate", choice);
    url = url.replace("_cuneate", choice);
    url = url.replace("_deltoid", choice);
    url = url.replace("_elliptic", choice);
    url = url.replace("_ensiform", choice);
    url = url.replace("_falcate", choice);
    url = url.replace("_filiform", choice);
    document[picName].src = url;

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Since replace expects a RegExp as the first argument (but accepts a string), use a regexp.

    Code:
    var re = /_(transparent|auriculate|cordate|...|filiform)/ig;
    url = url.replace( re choice);
    Put the rest of your values in where the ... is, of course.

    The /i flag is optional: It means "ignore case".
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    405
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Cool. that did the trick.

    is there a quick little tutorial somewhere that explains the "/ text1 | text2 | text3 /" bit that you put in? I haven't seen that before.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    Quote Originally Posted by turpentyne View Post
    Cool. that did the trick.

    is there a quick little tutorial somewhere that explains the "/ text1 | text2 | text3 /" bit that you put in? I haven't seen that before.
    The symbol | within a regular epression means or. Otherwise the symbol for or is || and for and is &&.

    There is a typo in Old Pedant's code:-

    url = url.replace( re choice);
    should read
    url = url.replace( re, choice);// note the comma
    Last edited by Philip M; 04-02-2013 at 03:15 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,273
    Thanks
    10
    Thanked 581 Times in 562 Posts
    here is a different way of doing it by using an array of matches instead of a RegExp.
    this makes it simpler to read and expand (avoiding RX's escapes) and it executes faster than RegExps as well.

    Code:
    var url = (document[picName].src);
    	   
    	
    function repAll( source, froms, to){
      var var i=0, mx=froms.length, it;
      for(i;i<mx;i++){
         source=source.split(r[i]).join(to);
      }
     return source;
    }
    
    
    var keys=["_transparent", "_auriculate", "_cordate", "_cuneate", 
                    "_deltoid", "_elliptic", "_ensiform", "_falcate", "_filiform"] ;
    
    document[picName].src = repAll(url, keys, choice );

    just another way to skin a cat in js...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Posting Permissions

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