javascript - JS RegEx challenge: replacing recurring patterns of commas and numbers -


i fiddling program convert japanese addresses romaji (latin alphabet) use in emergency broadcast system foreigners living in japanese city.

emergency evacuation warnings sent out lists of areas @ once. able copy/paste japanese list of areas , spit out romanized equivalent.

example japanese input: 3条4~12丁目、15~18条12丁目、2、3条5丁目 (this list of 3 areas, 条(jo) , 丁目(chome) indicate block numbers in north-south , east-west directions, respectively)

the numbers fine are, , have written code replace characters 条 , 丁目 romanized equivalents. program outputs first 2 areas (correctly) "3-jo 4~12-chome" , "15~18-jo 12-chome"

however, replace patterns in last area "2、5条6丁目" (meaning blocks 2 , 5 of 6-chome) such output reads "2&5-jo 6-chome"

the regular expression denotes pattern \d*、\d* (note japanese format comma)

i still getting used regex - how can replace comma found in \d*、\d* patterns "&"? note can't replace commas because used separate areas.

the easiest way isolate sequences 15、18 , replace commas in them.

text = "3条4~12丁目、15~18条12丁目、2、3条5丁目"; text.   replace(/(?:\d+、)+\d+/g, function(match) {     return match.replace(/、/g, "&");   }).   replace(/条/g, '-jō ').   replace(/丁目/g, '-chōme').   replace(/~/g, '-').   replace(/、/g, ', ') // => "3-jō 4-12-chōme, 15-18-jō 12-chōme, 2&3-jō 5-chōme" 

(also... heck live has 丁 well-ordered cardinal directions? live, addresses mess... :p )

(also also, sainaen nitpicking regexps perfection :) )


Comments