You can use a combination of [substring ()] [1] and [lastIndexOf ()] [2] to get the last element.
str = "~test content ~thanks ok ~fine"; strFine =str.substring(str.lastIndexOf('~')); console.log(strFine );
You can use [ split () ] [4] to convert a string to an array and get the element at the last index, the last index of length of array - 1 , since the array is an index based on zero.
str = "~test content ~thanks ok ~fine"; arr = str.split('~'); strFile = arr[arr.length-1]; console.log(strFile );
OR, just call pop in the array obtained after split
str = "~test content ~thanks ok ~fine"; console.log(str.split('~').pop());
Adil
source share