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
    419
    Thanks
    18
    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
    29,435
    Thanks
    85
    Thanked 4,912 Times in 4,873 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".
    Be yourself. No one else is as qualified.

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    419
    Thanks
    18
    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
    18,644
    Thanks
    208
    Thanked 2,593 Times in 2,571 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,608
    Thanks
    11
    Thanked 619 Times in 599 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...
    Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com


  •  

    Posting Permissions

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