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

//地址校验
$('#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;
}
})
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
可以使用以下代码来分隔省市区: ```javascript var address = "湖北省武汉市江夏区文化大道110号"; var reg = /.+?(省|市|自治区|自治州||区)/g; var addressParts = address.match(reg); var province = addressParts\[0\]; var city = addressParts\[1\]; var district = addressParts\[2\]; var town = addressParts\[3\]; console.log(province); // "湖北省" console.log(city); // "武汉市" console.log(district); // "江夏区" console.log(town); // "文化大道" ``` 这段代码使用正则表达式将地址按照省、市、区、进行分隔,并将分隔后的值存储在相应的变量中。你可以根据需要修改正则表达式来适应不同的地址格式。 #### 引用[.reference_title] - *1* [js截取省市区地址](https://blog.csdn.net/m0_54829066/article/details/128589086)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [JS 分割地址省市区自治区直辖市自治县)](https://blog.csdn.net/Fxhani/article/details/108605160)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [JS 地址截取 省市区 (自治区直辖市自治县)](https://blog.csdn.net/qq_36595944/article/details/106355065)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值