it depends a bit on the structure of the address. If you're addresses always have a country at the end, it will have a structure like this:
10469 5th Ave, New York, NY 10065, USA
Marienplatz 33, 80331 München, Germany
You can use this code:
var a = @Map.join().split(","); a.[a.length -1].replace(/[a-zA-Z" "]/g,"")
The code works like this:
- convert the address to a string (.join())
- look for commas as separator and split the string into an array of substrings (USA: 4 arrays, Germany: 3 arrays; the 1st array = 0, 2nd = 1 etc.)
- count the arrays (.length) and get the last but one array (which contains the zip in both examples)
- replace all letters with "" (nothing) and leave only numerals.
If you addresses are mixed (some have a country at the end, some not) you can first check if the last array contains letters only. And if there are letters only (= address has a Country cause a Country name never contains a numeral), the zip is in the last but one array, if there a numerals, the address doesn't have a Country and the zip is in the last array.
var a = @Map.join().split(",") var b = a[a.length-1] b.match(/[0-9]/) ? a[a.length-1].replace(/[a-zA-Z" "]/g,"") : a[a.length-2].replace(/[a-zA-Z" "]/g,"")