Extracting a string from another string Javascript

Issue

I have this particular string :

TOYIN KEMIOGS/OYO/2277TGOGSLAGOS

from this string containing 2 ‘/’

I want it to extract from wherever we have OGS and stop at wherever we have OGS. OGS always start and end my extracted string

I want an extracted result like this

OGS/OYO/2277TGOGS

Thanks so much

Solution

You can achieve it but as a string between first occurrence and last occurrence of OGS as follows:

var a = 'TOYIN KEMIOGS/OYO/2277TGOGSLAGOS';
console.log(a.slice(a.indexOf("OGS"),a.lastIndexOf("OGS")) +"OGS");

Answered By – Harshit Dewan

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published