Created
November 10, 2020 09:42
-
-
Save remino/6bc80255490e808e1b2dee2c778bf56e to your computer and use it in GitHub Desktop.
Parse Japanese address into an object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Use at your own risk. Will not work for all Japanese addresses. | |
const regex = /^(.+[都道府県])?(.+?[市郡区町村]+)([^0-90-9ー- -]*)([0-90-9]+[丁目番地のー--]*(?:[0-90-9]+[番ー--]?(?:[0-90-9]+号?)?)?)?[\s ー-~-]*(.*)?/ | |
const getMatch = (matches, index) => (matches ? matches[index] : null) || '' | |
const parseJpAddress = (str) => { | |
if (!str || !str.length) return {} | |
const matches = `${str}`.trim().match(regex) | |
const prefecture = getMatch(matches, 1) | |
const municipality = getMatch(matches, 2) | |
const area = getMatch(matches, 3) | |
const division = getMatch(matches, 4) | |
const buildingAndRoom = getMatch(matches, 5) | |
return { | |
prefecture, | |
municipality, | |
area, | |
division, | |
buildingAndRoom, | |
} | |
} | |
const input = process.argv.slice(2).join(' ') | |
console.log(input, JSON.stringify(parseJpAddress(input))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment