Get Substring between two characters using javascript

Rob picture Rob · Feb 14, 2013 · Viewed 335k times · Source

I am trying to extract a string from within a larger string where it get everything inbetween a : and a ;

Current

Str = 'MyLongString:StringIWant;'

Desired Output

newStr = 'StringIWant'

Answer

Babasaheb Gosavi picture Babasaheb Gosavi · Feb 14, 2013

You can try this

var mySubString = str.substring(
    str.lastIndexOf(":") + 1, 
    str.lastIndexOf(";")
);