简体   繁体   English

用JavaScript分割地址字符串

[英]Address String Split in javascript

Ok folks I have bombed around for a few days trying to find a good solution for this one. 好的,我炸弹了几天,试图为这个找到一个好的解决方案。 What I have is two possible address formats. 我有两种可能的地址格式。

28 Main St Somecity, NY 12345-6789    
or 
Main St Somecity, Ny 12345-6789

What I need to do Is split both strings down into an array structured as such 我需要做的是将两个字符串都分成一个结构如下的数组

address[0] = HousNumber 
address[1] = Street 
address[2] = City 
address[3] = State 
address[4] = ZipCode

My major problem is how to account for the lack of a house number. 我的主要问题是如何解决缺少门牌的问题。 with out having the whole array shift the data up one. 没有整个数组将数据上移一个。

address[0] = Street 
address[1] = City 
address[2] = State 
address[3] = ZipCode

[Edit] [编辑]

For those that are wondering this is what i am doing atm . 对于那些想知道这是我在做atm的人。 (cleaner version) (更干净的版本)

     place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
    FCmap.setCenter(point,12);


        var a = place.address.split(',');

        var e = a[2].split(" ");

        var x = a[0].split(" ");

        var hn = x.filter(function(item,index){
            return index == 0;
        });

                var st = x.filter(function(item,index){
                    return index != 0;
                });

                var street = ''; 

                st.each(function(item,index){street += item + ' ';});

        results[0] = new Hash({
                        FullAddie:  place.address,
                        HouseNum:   hn[0],
                        Dir:        '',
                        Street:     street,
                        City:       a[1],
                        State:      e[1],
                        ZipCode:    e[2],
                        GPoint:     new GMarker(point),
                        Lat:        place.Point.coordinates[1],
                        Lng:        place.Point.coordinates[0]
                        });
        // End Address Splitting 

Reverse the string, do the split and then reverse each item. 反转字符串,进行拆分,然后反转每个项目。

Update : From the snippet you posted, it seems to me that you get the address from a Google GClientGeocoder Placemark. 更新 :从您发布的摘录中,我看来您是从Google GClientGeocoder地标获得地址的。 If that is correct, why are you getting the unstructured address ( Placemark.address ) instead of the structured one ( Placemark.AddressDetails )? 如果是正确的话,为什么要获取非结构化地址( Placemark.address )而不是结构化地址( Placemark.AddressDetails )? This would make your life easier, as you would have to try and parse only the ThoroughfareName, which is the street level part of the address, instead of having to parse everything else as well. 这将使您的生活更轻松,因为您将不得不尝试仅解析ThoroughfareName(地址的街道级别部分),而不必同时解析其他所有内容。

function get_address (addr_str) {
   var m = /^(\d*)\s*([-\s\w.]+\s(?:St|Rd|Ave)\.?)\s+([-\s\w\.]+),\s*(\w+)\s+([-\d]+)$/i.exec(s);
   var retval = m.slice(1);
   if (!retval[0]) retval = retval.slice(1);
   return retval;
}

Assume all streets ends with St, Rd or Ave. 假设所有街道均以St,Rd或Ave结尾。

var address = /[0-9]/.match(string.charAt(0)) ? var地址= /[0-9]/.match(string.charAt(0))吗? string.split(" ") : [ " " ].concat(string.split(" ")); string.split(“”):[“”] .concat(string.split(“”));

This is not particularly robust, but it accounts for the two enumerated cases and is concise at only one line. 这不是特别可靠,但是它说明了两种情况,并且只在一行中简明扼要。

I've got a similar problem I'm trying to solve. 我也想解决类似的问题。 It seems that if you look for the first space to the right of the house number, you can separate the house number from the street name. 看起来,如果您在门牌号码的右边寻找第一个空格,则可以将门牌号码与街道名称分开。

Here in Boston you can have a house number that includes a letter! 在波士顿,您可以拥有包含字母的门牌号码! In addition, I've seen house numbers that include "1/2". 此外,我看到的门牌号包括“ 1/2”。 Luckily, the 1/2 is preceded by a hyphen, so there aren't any embedded spaces in the house number. 幸运的是,1/2前面带有连字符,因此门牌号中没有任何嵌入的空格。 I don't know if that's a standard or if I'm just getting lucky. 我不知道这是标准,还是很幸运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM