Skip to content

Instantly share code, notes, and snippets.

@remino
Created November 10, 2020 09:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save remino/6bc80255490e808e1b2dee2c778bf56e to your computer and use it in GitHub Desktop.
Save remino/6bc80255490e808e1b2dee2c778bf56e to your computer and use it in GitHub Desktop.
Parse Japanese address into an object
// 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