Solution 1 :

You can make use of Array.prototype.splice():

const addressName  = "My Business, 123 Main Street, Vancouver, BC, Canada";
const provinces = addressName.split(',').slice(2).join(',');
console.log(provinces);

Solution 2 :

Try this:

var addressName = "My Business, 123 Main Street, Vancouver, BC, Canada"
var output = addressName.split(",").slice(2).join(",")
console.log(output)

Problem :

I am looking to pull the city, province/state and country from a address field and I want to remove the business name and street name before as follows:

addressName = "My Business, 123 Main Street, Vancouver, BC, Canada"

I want to capture this:

output = "Vancouver, BC, Canada"

I have tried splitting the array using the comma as the entry point although in that case I only get one of the three pieces of information that I need and I have done so in order to capture just the business name in a separate field. Will I need to have a range for the array to go through? Any help is appreciated, thank you!

Comments

Comment posted by Roberto Zvjerković

addressName.split(',').slice(2).join(',').trim()

Comment posted by Zain

That makes sense, thank you very much!

Comment posted by Tushar Shahi

You split using comma and remove the first two elements. Once again you join.

Comment posted by epascarello

"My Business, 123 Main Street, Vancouver, BC, Canada".replace(/(.+, )([^,]+,[^,]+,[^,]+)$/, '$2')

Comment posted by How to Answer

IDidn’tDownVote

By