Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript get characters between slashes

Can someone please help. I need to get the characters between two slashes e.g:

Car/Saloon/827365/1728374

I need to get the characters between the second and third slashes. i.e 827365

like image 399
echez Avatar asked Dec 15 '11 11:12

echez


1 Answers

You can use the split() method of the String prototype, passing in the slash as the separator string:

const value = 'Car/Saloon/827365/1728374';
const parts = value.split('/');

// parts is now a string array, containing:
// [ "Car", "Saloon", "827365", "1728374" ]
// So you can get the requested part like this:

const interestingPart = parts[2];

It's also possible to achieve this as a one-liner:

const interestingPart = value.split('/')[2];

Documentation is here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split

like image 135
Anders Marzi Tornblad Avatar answered Sep 29 '22 02:09

Anders Marzi Tornblad