Rocker rock
Rocker rock

Reputation: 155

Split destination into city, region and country

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

Answers (2)

Abhishek Gurjar
Abhishek Gurjar

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

Amiga500
Amiga500

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

Related Questions