Hello. How can you break an array into two parts in the right place, if the element of the desired index is repeated. For example, I need to get the last number in the expression 45-23 * 996-25 (how to break an array at the place of the second minus)? Thank you in advance

    2 answers 2

    Use split to split and pop to take the last element.

    split - allows you to turn a string into an array, breaking it by separator.

    pop - removes the last element from the array and returns its value.

     var test = '45-23*996-25'; var last = test.split('-').pop(); console.log(last); 

      If a

      need to get the last number

      then you can

       var test = '45-23*996-25'; var last = test.substr(test.lastIndexOf('-')+1); console.log(last)