I am currently trying to find matches in the given strings:
'Los Angeles, CA'
'New York, NY'
'Williamsburg, Brooklyn, NY'
by comparing them with the following input strings:
'Los Angeles, CA 90001, USA'
'New York, NY 10017, USA'
'Williamsburg, Brooklyn, NY, USA'
This task is being tackled using JavaScript. Here's what I have so far:
var re = /.+[A-Z]{2}( |,)/,
location = '', // Insert address string here
substring = location.match(re)[0]; // This will return something like Williamsburg, Brooklyn, NY,
While the regular expression above successfully matches the examples provided, it leaves a trailing whitespace or comma at the end depending on the string. I could use slice()
to remove the trailing character, but I am wondering if this can be achieved within the regex itself. Is it possible?