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

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

JavaScript and AJAX Forum

Converting long list of abbreviations

 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:


And without messing up the string:

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



 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.


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

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


 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(" ");


 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]);}


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