-2

I have a String like the following pattern:

string1 / string2 / string3

I want to get the following as end string:

string2 / string3

I am doing that with the code below. Is there a better way in Javascript to manage that?

code

var key = 'door / chair / screw';
var keyArray = key.replace('/', ', ');
var endKey = keyArray[1];
31piy
  • 23,323
  • 6
  • 47
  • 67
benz
  • 693
  • 1
  • 9
  • 29

1 Answers1

1

A simple way to do this is by using split, slice and join.

var key = 'door / chair / screw';
console.log(key.split(' / ').slice(1).join(' / '));

Or if you want to do it by replace, here is a way:

var key = 'door / chair / screw';
console.log(key.replace(/\w+\s+\/\s+/, ''));
31piy
  • 23,323
  • 6
  • 47
  • 67