Welcome to WebmasterWorld Guest from 54.162.8.162

Forum Moderators: open

Message Too Old, No Replies

Converting long list of abbreviations

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

Full Member

5+ Year Member Top Contributors Of The Month

joined:Sept 30, 2009
posts:226
votes: 1


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"
5:20 pm on Aug 25, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 15, 2003
posts:922
votes: 16


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)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 3, 2005
posts:1585
votes: 0


Yes, and only apply change to word in correct position
eg
3 St Mary St => 3 St Mary Street
3:45 am on Aug 26, 2012 (gmt 0)

Full Member

5+ Year Member Top Contributors Of The Month

joined:Sept 30, 2009
posts:226
votes: 1


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)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 3, 2005
posts:1585
votes: 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 ?