js分割地址 省市区镇小区单元楼门牌号 (含自治区,直辖市,县,自治县)

本文介绍了一种用于解析中文地址的算法,该算法能够将输入的完整地址拆分为省份、城市、区县、乡镇、地标、建筑、单元、楼层及门牌号等详细组成部分,适用于物流、地产等行业对地址进行标准化处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//地址校验
$('#address').textbox({
	onChange:function(newValue,oldValue){
		var address = newValue;
		var area = {}
	    var index11 = 0
	    var index1 = address.indexOf("省")
	    //alert(index1)
	    if (index1 == -1) {
	      index11 = address.indexOf("自治区")
	      if (index11 != -1) {
	        area.Province = address.substring(0, index11 + 3)//有值得时候取到自治区
	      } else {
	        area.Province = address.substring(0, 0)//没值
	      }
	    } else {
	      area.Province = address.substring(0, index1 + 1)//取到省
	    }
	 
	    var index2 = address.indexOf("市")
	    if (index11 == -1) {
	      area.City = address.substring(index11 + 1, index2 + 1);//没有市就取到省
	    } else {
	      if (index11 == 0) {
	        area.City = address.substring(index1 + 1, index2 + 1);//有值从省取到市
	      } else {
	        area.City = address.substring(index11 + 3, index2 + 1);//从自治区取到市
	      }
	    }
	 	//var index5 = 0
	    var index3 = address.indexOf("区")
	    if (index3 == -1) {
	      index3 = address.indexOf("县");
	      area.Country = address.substring(index2 + 1, index3 + 1)
	    } else {
	      area.Country = address.substring(index2 + 1, index3 + 1)
	    }
	   
	    var index4 = address.indexOf("镇")
	    if (index4 == -1) {
	      area.Town = address.substring(index3 + 1, index4 + 1);//从区到镇
	    } else {
	      if (index4 == 0) {
	        area.Town = address.substring(index3 + 1, index4 + 1);//区到镇
	      } else {
	        area.Town = address.substring(index3 + 1 , index4 + 1);//
	      }
	    }
	    
	    var index5 = address.indexOf("号")
	    if (index5 == -1) {
	      area.Mark = address.substring(index4 + 1, index5 + 1);
	    } else {
	      if (index5 == 0) {
	        area.Mark = address.substring(index4 + 1, index5 + 1);
	      } else {
	        area.Mark = address.substring(index4 + 1 , index5 + 1);
	      }
	    }
	    
	    var index6 = address.indexOf("号楼")
	    if (index6 == -1) {
	      area.building = address.substring(index5 + 1, index6 + 2);
	    } else {
	      if (index6 == 0) {
	        area.building = address.substring(index5 + 1, index6 + 2);
	      } else {
	        area.building = address.substring(index5 + 1 , index6 + 2);
	      }
	    }
	    
	    var index7 = address.indexOf("单元")
	    if (index7 == -1) {
	      area.unit = address.substring(index6 + 2, index7 + 2);
	    } else {
	      if (index7 == 0) {
	        area.unit = address.substring(index6 + 2, index7 + 2);
	      } else {
	        area.unit = address.substring(index6 + 2 , index7 + 2);
	      }
	    }
	    
	    var index8 = address.indexOf("层")
	    if (index8 == -1) {
	      area.floor = address.substring(index7 + 2, index8 + 1);
	    } else {
	      if (index8 == 0) {
	        area.floor = address.substring(index7 + 2, index8 + 1);
	      } else {
	        area.floor = address.substring(index7 + 2 , index8 + 1);
	      }
	    }
	    
	    var index9 = address.lastIndexOf("号")
	    if (index9 == -1) {
	      area.number = address.substring(index8 + 1, index9 + 1);
	    } else {
	      if (index9 == 0) {
	        area.number = address.substring(index8 + 1, index9 + 1);
	      } else {
	        area.number = address.substring(index8 + 1 , index9 + 1);
	      }
	    }
	    
	    
		 //alert(area.number)
		
		
	    return area;
}
})

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值