- 解析地址字符串,严格匹配【省市区】、【市市区】、【自治区市区】格式
- 格式要求:
-
- 省市区:XX省XX市XX区/县
-
- 市市区:XX市XX市XX区/县 (仅限直辖市)
-
- 自治区市区:XX自治区XX市XX区/县
- 符合上述格式则返回剩余部分(乡镇/街道及以下),否则返回原地址
function parseAddress(address) {const municipalities = ['北京市', '天津市', '上海市', '重庆市'];const strictRegex = /^((?<province>[^省]+省)(?<city>[^市]+市)(?<county>[^市县区旗]+(?:区|县|旗))| (?<municipality>北京市|天津市|上海市|重庆市)(?<district>[^市]+市)(?<county2>[^市县区旗]+(?:区|县|旗))| (?<province2>.+自治区)(?<city2>[^市]+市)(?<county3>[^市县区旗]+(?:区|县|旗)) )(?<rest>.*)$/;const provinceCount = (address.match(/省/g) || []).length;const autonomousRegionCount = (address.match(/自治区/g) || []).length;if (provinceCount > 1 || autonomousRegionCount > 1 || (provinceCount > 0 && autonomousRegionCount > 0)) {return address;}const normalizedAddr = address.replace(/[^\u4e00-\u9fa50-9a-zA-Z]/g, '');const match = strictRegex.exec(normalizedAddr);if (match?.groups) {const isProvinceFormat = match.groups.province && match.groups.city && match.groups.county;const isMunicipalityFormat = match.groups.municipality && match.groups.district && match.groups.county2;const isAutonomousFormat = match.groups.province2 && match.groups.city2 && match.groups.county3;if (isProvinceFormat || isMunicipalityFormat || isAutonomousFormat) {if (isMunicipalityFormat && !municipalities.includes(match.groups.municipality)) {return address;}return match.groups.rest || '';}}return address;
}