// 识别 地址的方法 splitAddressInfo(address) { address = address.replace(/[^\u4E00-\u9FA5a-zA-Z0-9]/g, ''); const nameRegex = /(.+?)(\d+)/; const contactRegex = /(\d{11})/; const regionRegex = /(.*?[省市区县])(.*?[市区县])(.*)/; const districtRegex = /(.*?[区县])(.*)/; const provinceRegex = /(.*?[省市自治区])(.*)/; let name = ''; let addressText = ''; let contact = ''; let province = ''; let city = ''; let district = ''; let detailedAddress = ''; const nameMatch = address.match(nameRegex); if (nameMatch && nameMatch.length >= 3) { name = `${nameMatch[1]}`; } const contactMatch = address.match(contactRegex); if (contactMatch && contactMatch.length >= 2) { contact = contactMatch[1]; } const regionMatch = address.match(regionRegex); if (regionMatch && regionMatch.length >= 4) { province = regionMatch[1]; city = regionMatch[2]; district = regionMatch[3]; } const districtMatch = district.match(districtRegex); if (districtMatch && districtMatch.length >= 3) { district = districtMatch[1]; detailedAddress = districtMatch[2]; } const provinceMatch = address.match(provinceRegex); if (provinceMatch && provinceMatch.length >= 3) { province = provinceMatch[1]; detailedAddress = provinceMatch[2]; } addressText = address .replace(nameRegex, '') .replace(contactRegex, '') .replace(regionRegex, '') .replace(districtRegex, '') .replace(provinceRegex, '') .trim(); // 拆出区后面地址的正则 const detailAddressRegexy = new RegExp(`${province}${city}${district}(.*)`); const match1 = address.match(detailAddressRegexy); // 拆出省份 的正则 const provinceRegexs = /([\u4e00-\u9fa5]{2,}(?:省|市|自治区|特别行政区))/; const match = province.match(provinceRegexs); province = match[1] // console.log(match1); this.name = name this.phone = contact this.province = province this.city = city this.district = district this.addressXI = province + '-' + city + '-' + district this.xingxi = match1[1] },
然后可以根据自己实际场景,进行修改,主要用于快递地址自动识别,上面方法写的比较笼统,个人可以在原有上进行二次封装开发
标签:province,const,district,地址,let,拆分,address,省市区,match From: https://www.cnblogs.com/yangjunp/p/17437062.html