How to split a string by dash at specific position JavaScript

I have a dynamically created array as below:

["171281-0-001-1", "171281-0-001-2"]

Basically, I am creating the value based on a very complex function. But that function does not matter here. Basically for example 171281-0-001-1 is made of Account number + id. 171281-0-001 is Account number and 1 is the id. The number of dashes can be any number but after the last dash it's always id. How can I differentiate these from my value and assign it into different variables?


for (let key in this.your_reference) {
  extractedAccountNumber = key.split('-')[0];
  extractedId = key.split('-')[1];

But since the dashes can be in any number now, the variables have wrong values. Any idea guys? Thanks in advance


5 Likes8.50 GEEK