I have string like this
This is ~test content ~ok ~fine.
I want to get "fine"
which is after special character ~
and on last position in string using jQuery.
You can use 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 the string to array and get the element at last index, last index is length of array - 1
as array is zero based index.
str = "~test content ~thanks ok ~fine";
arr = str.split('~');
strFile = arr[arr.length-1];
console.log(strFile );
OR, simply call pop on array got after split
str = "~test content ~thanks ok ~fine";
console.log(str.split('~').pop());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With