I have a string of the following format:
var str1="tag:youtube.com,2008:video:VrtMalb-XcQ";
From this I need to extract the last part i.e. VrtMalb-XcQ. How can I achieve this?
I have a string of the following format:
var str1="tag:youtube.com,2008:video:VrtMalb-XcQ";
From this I need to extract the last part i.e. VrtMalb-XcQ. How can I achieve this?
What you need to know is the index of the last :. This can be achieved by using lastIndexOf
var str1="tag:youtube.com,2008:video:VrtMalb-XcQ";
var result = str1.substr(str1.lastIndexOf(':') + 1);
The + 1 is there because otherwise you would also get the colon itself in the string.
This is really the fastest approach to do it, and requires the least memory.
use split and pop:
var str1="tag:youtube.com,2008:video:VrtMalb-XcQ";
str1.split(':').pop()
"tag:youtube.com,2008:video:VrtMalb-XcQ".replace(/.*:/, "")