homepage Welcome to WebmasterWorld Guest from 54.167.249.155
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Converting long list of abbreviations
ocon




msg:4488276
 3:53 pm on Aug 25, 2012 (gmt 0)

I have a standard mailing address variable, that could be equal to such strings as:

"565 N Clinton Dr"
"740 Evergreen Ter"
"31 Spooner St"

I'd like to expand these addresses using JavaScript and a list of standard mailing abbreviations (http://www.gis.co.clay.mn.us/usps.htm) into something more readable.

"565 North Clinton Drive"
"740 Evergreen Terrace"
"31 Spooner Street"

I'm trying to figure out how I can efficiently convert these variables without using a long replace string:

address.replace("N","North").replace("Dr","Drive").replace("Ter","Terrace").reaplce("St","Street")...;

And without messing up the string:

"123 Nancy St" => "123 Northancy Street"
"456 Terracotta Drive" => "456 Terraceracotta Driveive"

 

rainborick




msg:4488294
 5:20 pm on Aug 25, 2012 (gmt 0)

Use .split to split the string into words and then do your replacements on the individual words. When you've processed all of the individual words, use .join to reconstruct the string.

daveVk




msg:4488360
 1:19 am on Aug 26, 2012 (gmt 0)

Yes, and only apply change to word in correct position
eg
3 St Mary St => 3 St Mary Street

ocon




msg:4488372
 3:45 am on Aug 26, 2012 (gmt 0)

Thank you for the replies. Dave, thankfully these mailing addresses only use official mailing abbreviations so I don't have to worry about that.

So would I use the following setup?

var address = "789 N Silicon Aly";
var abbreviations={"Aly":"Alley","Anx":"Annex","Arc":"Arcade","Ave":"Avenue","Byu":"Bayoo","Bch":"Beach",...}

address = address.split(" ");

for(var i = 0; i < address.length; i++){
for(var val in abbreviations) address[i] = address[i].split(val).join(abbreviations[val]);}

address = address.join(" ");

daveVk




msg:4488563
 4:50 am on Aug 27, 2012 (gmt 0)

instead of

for(var val in abbreviations) address[ i ] = address[ i ].split(val).join(abbreviations[val]);}

try

if (abbreviations[address[ i ]] !== undefined ) // have abbrev
{ address[ i ] = abbreviations[address[ i ]]; } // use it

is "3 Arc Ave" valid ?

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved