我有一个字符串
var str = "Orlando (and vicinity), Florida, United States of America";
我想将此字符串拆分为Javascript中的城市,地区和国家/地区。
Eg: city - Orlando
region - Florida
country - United States Of America
答案 0 :(得分:1)
尝试使用以下内容,我已将它们插入对象中。
var str = "Orlando (and vicinity), Florida, United States of America";
data = str.split(',')
city = data[0].match(/^\w+/g)
res = {
'city': city[0],
'region': data[1],
'country': data[2]
}
console.log(res)

答案 1 :(得分:0)
这将创建一个对象,该对象包含key:values,基于split:
let str = "Orlando (and vicinity), Florida, United States of America";
let locationData = {city: "", region: "", country: ""};
let result = str.split(",")
locationData.city = result[0];
locationData.region = result[1];
locationData.country = result[2];
然而,像这样拆分字符串是不可靠的。