快递地址解析思路和实现脚本

快递地址的内容主要有3项:姓名、手机和联系地址,开始以为从一段文字中提取这些内容不是什么难事,但真操作起来发现并没那么简单,总结了下,我们平时输写的地址格式主要有以下几种:

引用内容 引用内容
①.张三 13800000000 浙江省杭州市西湖区北山街1号
②.张三,13800000000,浙江省杭州市西湖区北山街1号
③.张三13800000000浙江省杭州市西湖区北山街1号
④.浙江省杭州市西湖区北山街1号 张三 13800000000
⑤.浙江省 杭州市 西湖区 北山街1号 张三 13800000000
⑥.张三 浙江省 杭州市 西湖区 北山街1号 13800000000
⑦.张三浙江省杭州市西湖区北山街1号13800000000
⑧.浙江省杭州市西湖区北山街1号张三13800000000

下边为我思考后得出的几种解析方法:

方法一:借助手机号位置解析法



var adrFormat = function(adr){
    let [name, tel, address] = ["", "error-"+adr, ""];
    let pvs = ["安徽","北京","重庆","福建","甘肃","广东","广西","贵州","海南","河北","河南","黑龙江","湖北","湖南","吉林","江苏","江西","辽宁","内蒙古","宁夏","青海","山东","山西","陕西","上海","四川","天津","西藏","新疆","云南","浙江"];
    adr = adr.trim();
    adr = adr.replace(/\s+/g, ",").replace(/,/g, ",").replace(/,+/g, ",");
    adr = adr.replace(/[^-,\w\u4e00-\u9fa5]/g, "");
    adr = adr.replace(/(收货人)|(手机号码)|(所在地区)|(详细地址)/g,""); //淘宝专用
    let regTel = /[\d\-]{7,}/;
    if((/[^\d]+[\d\-]{7,}[^\d]+/).test(adr)){
        adr = adr.replace(/,/g,"");
        tel = regTel.exec(adr)[0];
        name = adr.split(tel)[0];
        address = adr.split(tel)[1];
    }else if((/^[\d\-]{7,}/).test(adr) || (/[\d\-]{7,}$/).test(adr)){
        tel = regTel.exec(adr)[0];
        adr = adr.replace(tel,"").replace(/^,/g,"").replace(/,$/g,"");                    
        let arr = adr.split(",");
        if(arr.length == 2){
            name = arr[0];
            address = arr[1];
        }else if(arr.length > 2){
            for(let item of pvs){
                if(adr.indexOf(item) != -1){
                    name = (adr.indexOf(item) == 0) ? arr[arr.length - 1] : arr[0];
                    address = arr.join("").replace(name, "");
                    break;
                }
            }
        }else{
            for(let item of pvs){
                if(adr.indexOf(item) > 0){
                    address = adr.substring(adr.indexOf(item));
                    name = adr.replace(address, "");
                    break;
                }
            }
        }
    }

    if(name.length > address.length){
        [name, address] = [address, name];
    }
    return [name, tel, address];
}

方法二:升级优化



var adrFormat = function(adr){
    let [name, tel, address] = ["", "error-"+adr, ""];
    let pvs = ["安徽","北京","重庆","福建","甘肃","广东","广西","贵州","海南","河北","河南","黑龙江","湖北","湖南","吉林","江苏","江西","辽宁","内蒙古","宁夏","青海","山东","山西","陕西","上海","四川","天津","西藏","新疆","云南","浙江"];
    adr = adr.trim();
    adr = adr.replace(/\s+/g, ",").replace(/,/g, ",").replace(/,+/g, ",");
    adr = adr.replace(/[^-,\w\u4e00-\u9fa5]/g, "");
    adr = adr.replace(/(收货人)|(手机号码)|(所在地区)|(详细地址)/g,""); //淘宝专用
    let regTel = /[\d\-]{7,}/;    
    if(regTel.test(adr)){
        tel = regTel.exec(adr)[0];
        adr = adr.replace(tel,",").replace(/,+/g, ",").replace(/^,/g,"").replace(/,$/g,"");
        let arr = adr.split(",");
        if(arr.length == 2){
            name = arr[0];
            address = arr[1];
        }else if(arr.length > 2){
            for(let item of pvs){
                if(adr.indexOf(item) != -1){
                    name = (adr.indexOf(item) == 0) ? arr[arr.length - 1] : arr[0];
                    address = arr.join("").replace(name, "");
                    break;
                }
            }
        }else{
            for(let item of pvs){
                if(adr.indexOf(item) > 0){
                    address = adr.substring(adr.indexOf(item));
                    name = adr.replace(address, "");
                    break;
                }
            }
        }
    }

    if(name.length > address.length){
        [name, address] = [address, name];
    }
    return [name, tel, address];
}


评论: 0 | 引用: 0 | 查看次数: 2398
发表评论
登录后再发表评论!