简体   繁体   中英

How to convert data in another form using Regex?

I have a huge list with all the countries and cities in this format below.

city_states['Finland'] = '|Helsinki||Aland|Etela-Suomen Laani|Ita-Suomen Laani|Lansi-Suomen Laani|Lappi|Oulun Laani';

My question is how can I convert it into so that I can run it in an autocomplete script? I use notepad++ so it lets me to do regex.

{ value: 'Finland, Helsinki', data: '' },
{ value: 'Finland, Aland', data: '' },
...
...
...

Thank you very much.

Iterate the city_states variable and then push objects into result array.

 var city_states = {}; city_states['Finland'] = '|Helsinki||Aland|Etela-Suomen Laani|Ita-Suomen Laani|Lansi-Suomen Laani|Lappi|Oulun Laani'; var result = []; for (var k in city_states) { var c = city_states[k].replace(/^\\|/, "").split(/[\\|]+/) for (var i in c) { result.push({ value: k + ", " + c[i], data: "" }); } } alert(JSON.stringify(result, null, 2)); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM