Reputation: 155
I have a string
var str = "Orlando (and vicinity), Florida, United States of America";
I want to split this string to hold city, region and country in Javascript.
Eg: city - Orlando
region - Florida
country - United States Of America
Upvotes: 0
Views: 168
Reputation: 7476
Try with below, I've inserted them in a object.
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)
Upvotes: 1
Reputation: 6131
This will create an object that holds the key:values, based on the 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];
However splitting the string like this is not reliable.
Upvotes: 0