Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-02-2013, 03:43 AM   PM User | #1
turpentyne
Regular Coder

 
Join Date: Aug 2010
Posts: 316
Thanks: 10
Thanked 1 Time in 1 Post
turpentyne is an unknown quantity at this point
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;
turpentyne is offline   Reply With Quote
Old 04-02-2013, 04:33 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,055 Times in 4,024 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 04-02-2013, 03:04 PM   PM User | #3
turpentyne
Regular Coder

 
Join Date: Aug 2010
Posts: 316
Thanks: 10
Thanked 1 Time in 1 Post
turpentyne is an unknown quantity at this point
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.
turpentyne is offline   Reply With Quote
Old 04-02-2013, 03:10 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,101
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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
__________________

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.

Last edited by Philip M; 04-02-2013 at 03:15 PM..
Philip M is offline   Reply With Quote
Old 04-02-2013, 10:22 PM   PM User | #5
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,553
Thanks: 9
Thanked 480 Times in 463 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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 5/13)
STATS (2013/5) HTML5:90.2% MOB:15.2% IE7:0.5% IE8:8.4% IE9:8.5% IE10:8.5%
rnd me is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:06 AM.


Advertisement
Log in to turn off these ads.