方法一:
通过js进行处理
var str = "湖北省武汉市江夏区文化大道110号";
console.log(that.getArea(str));
//省市区截取
getArea: function(str) {
let area = {}
let index11 = 0
let index1 = str.indexOf("省")
if (index1 == -1) {
index11 = str.indexOf("自治区")
if (index11 != -1) {
area.Province = str.substring(0, index11 + 3)
} else {
area.Province = str.substring(0, 0)
}
} else {
area.Province = str.substring(0, index1 + 1)
}
let index2 = str.indexOf("市")
if (index11 == -1) {
area.City = str.substring(index11 + 1, index2 + 1)
} else {
if (index11 == 0) {
area.City = str.substring(index1 + 1, index2 + 1)
} else {
area.City = str.substring(index11 + 3, index2 + 1)
}
}
let index3 = str.lastIndexOf("区")
if (index3 == -1) {
index3 = str.indexOf("县")
area.Country = str.substring(index2 + 1, index3 + 1)
} else {
area.Country = str.substring(index2 + 1, index3 + 1)
}
return area;
}
可以看到结果如下:

方法二:
通过正则进行判断
var str = "湖北省武汉市江夏区文化大道110号";
var reg = /.+?(省|市|自治区|自治州|县|区)/g; // 省市区的正则
console.log(str.match(reg));
最终的结果如下:

本文介绍两种使用JavaScript处理和解析详细地址的方法,一种是通过字符串操作截取省市区信息,另一种是利用正则表达式匹配地址组件。适用于前端开发中地址信息的自动化处理。
&spm=1001.2101.3001.5002&articleId=108605160&d=1&t=3&u=2f4f7efde2db4ab3a5c96b7173f06cbf)
497

被折叠的 条评论
为什么被折叠?



