Search code examples
javascriptregexstringsubstring

Get Substring between two characters using JavaScript


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

Current

Str = 'MyLongString:StringIWant;'

Desired Output

newStr = 'StringIWant'

Solution

  • You can try this

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