Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 30th, 2012  |  syntax: None  |  size: 3.15 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Help parsing string (City, State Zip) with JavaScript
  2. var address = "San Francisco, CA 94129";
  3.  
  4. function parseAddress(address) {
  5.     // Make sure the address is a string.
  6.     if (typeof address !== "string") throw "Address is not a string.";
  7.  
  8.     // Trim the address.
  9.     address = address.trim();
  10.  
  11.     // Make an object to contain the data.
  12.     var returned = {};
  13.  
  14.     // Find the comma.
  15.     var comma = address.indexOf(',');
  16.  
  17.     // Pull out the city.
  18.     returned.city = address.slice(0, comma);
  19.  
  20.     // Get everything after the city.
  21.     var after = address.substring(comma + 2); // The string after the comma, +2 so that we skip the comma and the space.
  22.  
  23.     // Find the space.
  24.     var space = after.lastIndexOf(' ');
  25.  
  26.     // Pull out the state.
  27.     returned.state = after.slice(0, space);
  28.  
  29.     // Pull out the zip code.
  30.     returned.zip = after.substring(space + 1);
  31.  
  32.     // Return the data.
  33.     return returned;
  34. }
  35.  
  36. address = parseAddress(address);
  37.        
  38. function parseAddress(a) {if(typeof a!=="string") throw "Address is not a string.";a=a.trim();var r={},c=a.indexOf(',');r.city=a.slice(0,c);var f=a.substring(c+2),s=f.lastIndexOf(' ');r.state=f.slice(0,s);r.zip=f.substring(s+1);return r;}
  39.        
  40. var parts = "City, State ZIP".split(/s+/); // split on whitespace
  41. var city = parts[0].slice(0, parts[0].length - 1); // remove trailing comma
  42. var state = parts[1];
  43. var zip = parts[2];
  44.        
  45. var parts = "san fran bay, new mex state 666666".split(/s+|,/),
  46.     partition = parts.indexOf(""),
  47.     city = parts.slice(0, partition).join(" "),
  48.     state = parts.slice(partition + 1, -1).join(" "),
  49.     zip = parts.pop();
  50.        
  51. var city, statezip, state, zip, parts;
  52. [city, statezip] = "Spaced City, New Mexico ZIP".split(/,s*/);
  53. parts = statezip.split(/s+/);
  54. zip = parts.pop();
  55. state = parts.join(" ");
  56.        
  57. var str = "New York, NY 20101";
  58.     var cityAndRest = str.split(',');
  59.     var city = cityAndRest[0];
  60.     var stateAndZip = cityAndRest[1].trim().split(' ');
  61.     var state = stateAndZip[0];
  62.     var zip = stateAndZip[1];
  63.        
  64. function IsNumeric(n) {
  65.   return !isNaN(parseFloat(n)) && isFinite(n);
  66. }
  67.  
  68. var addr = 'New York City, New York 10101';
  69. //var addr = 'San Bernadino, CA 11111';
  70. function getCityStateZip(addr){
  71.   var city; var state;var zip;
  72.   city = ''; state = ''; zip = '';
  73.   var addrLen = addr.length;
  74.   if ( IsNumeric( addr.substring(addrLen - 1) ) ) {
  75.     //contains a zipcode - just a sanity check
  76.     //get last 10 characters for testing easily
  77.     var lastTen = addr.substring( addrLen - 10 );
  78.     if ( lastTen.indexOf('-') > 0 && ( lastTen.indexOf(' ') == -1 ) ) {
  79.       //found a hyphen and no space (matches our complex rule for zipcodes)
  80.       zip = lastTen;
  81.     } else {
  82.       zip = addr.substring( addrLen - 5 ); //assume a basic 5 zip code
  83.     }
  84.   }
  85.   var zipLen = zip.length;
  86.   addrLen = addrLen - zipLen - 1;
  87.   addr = addr.substring(0, addrLen ); //remove the chars we just moved into zip
  88.  
  89.   var lastComma = addr.lastIndexOf(',');
  90.   if ( lastComma == -1 ) {
  91.     //you have a problem, how do you want to handle it?
  92.   }
  93.   city = addr.substring(0,lastComma); //skip the comma itself, yes?
  94.   state = addr.substring(lastComma + 2);
  95.   return { 'city':city,'state': state,'zip': zip};
  96. }
  97.  
  98. getCityStateZip(addr)