Kevin Smith
Kevin Smith

Reputation: 45

javascript regular expression to grab the state of 2 characters that is always following a city and a comma

Seems that google api for returns city and state from a zip code is just poorly made as they return a json object with address_components of which all have array list of elements with long_name and short_name

Now that would be fine , except that i cannot count on the state because in 3rd or the 4th array. :/

The ONE thing that i do see in common is a formatted_address of which example is

formatted_address: "Lake Villa, IL 60046, USA"

Since that is the case then seems that a regex is what i want

GOAL

var city = "Lake Villa"
var state = "IL" 

I don't want to replace.... here is some regex i was trying to work with

 var formatted_address = "Lake Villa, IL 60046, USA";
 alert(formatted_address.replace(/(.+)\-([A-Z]{2})$/, "$1, $2").replace("\-", " "));

Upvotes: 1

Views: 188

Answers (4)

Pedreiro
Pedreiro

Reputation: 1804

I recommend searching first at Txt2RE.com. It had an interesting regex for that:

<script language=javascript>
  var txt='Lake Vila, IL 60046, USA';

  var re1='(.*?),'; // Command Seperated Values 1
  var re2='(,)';    // Any Single Character 1
  var re3='(\\s+)'; // White Space 1
  var re4='((?:(?:AL)|(?:AK)|(?:AS)|(?:AZ)|(?:AR)|(?:CA)|(?:CO)|(?:CT)|(?:DE)|(?:DC)|(?:FM)|(?:FL)|(?:GA)|(?:GU)|(?:HI)|(?:ID)|(?:IL)|(?:IN)|(?:IA)|(?:KS)|(?:KY)|(?:LA)|(?:ME)|(?:MH)|(?:MD)|(?:MA)|(?:MI)|(?:MN)|(?:MS)|(?:MO)|(?:MT)|(?:NE)|(?:NV)|(?:NH)|(?:NJ)|(?:NM)|(?:NY)|(?:NC)|(?:ND)|(?:MP)|(?:OH)|(?:OK)|(?:OR)|(?:PW)|(?:PA)|(?:PR)|(?:RI)|(?:SC)|(?:SD)|(?:TN)|(?:TX)|(?:UT)|(?:VT)|(?:VI)|(?:VA)|(?:WA)|(?:WV)|(?:WI)|(?:WY)))(?![a-z])';    // US State 1
  var re5='(\\s+)'; // White Space 2
  var re6='(\\d+)'; // Integer Number 1
  var re7='(,)';    // Any Single Character 2
  var re8='(\\s+)'; // White Space 3
  var re9='(USA)';  // Word 1

  var p = new RegExp(re1+re2+re3+re4+re5+re6+re7+re8+re9,["i"]);
  var m = p.exec(txt);
  if (m != null)
  {
      var usstate1=m[4];
  }
</script>

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626932

Capture 1 or more chars other than , from the start into one group and then match a comma with whitespaces and then capture 2 ASCII letters as a whole word:

var formatted_address = "Lake Villa, IL 60046, USA";
var res = formatted_address.match(/^([^,]+),\s*([A-Z]{2})\b/);
if (res) {
  console.log(res[1]);
  console.log(res[2]);
}

Pattern details:

  • ^ - start of string
  • ([^,]+) - Group 1 (city): 1 or more chars other than ,
  • , - a comma
  • \s* - 0+ whitespaces
  • ([A-Z]{2}) - 2 uppercase ASCII letters
  • \b - a word boundary, the 2 letters must be followed with a non-word char or end of string.

Upvotes: 4

Arnav Aggarwal
Arnav Aggarwal

Reputation: 789

Here you go.

var str = "Lake Villa, IL 60046, USA";
var matches = str.match(/(.+?), (..)/);

var city = matches[1];
var state = matches[2];

console.log(city, state); // -> Lake Villa IL

Or in ES6:

const str = "Lake Villa, IL 60046, USA";
const [_, city, state] = str.match(/(.+?), (..)/);

console.log(city, state); // -> Lake Villa IL

Upvotes: 2

Edon
Edon

Reputation: 1216

Why not try splitting on commas, and just getting the values from the resulting array?

let city, state;

let input = "Lake Villa, IL 60046, USA";

input = input.split(",");

city = input[0];

state = input[1].replace(/[0-9]/g, '').trim(); // removes the numbers, and excess whitespace

console.log(city,state);

This results in:

state = IL
city = Lake Villa

Codepen here

Upvotes: 2

Related Questions