Skip to main content

Split a string and get last match in jQuery and Javascript

Method 1:
str = "~test content ~thanks ok ~eabhyas.blogspot.com";   
strFine =str.substring(str.lastIndexOf('~'));
console.log(strFine );

Output: ~eabhyas.blogspot.com


Method 2:
str = "~test content ~thanks ok ~eabhyas.blogspot.com";   
console.log(str.split('~').pop());

OR
str = "~test content ~thanks ok ~eabhyas.blogspot.com"; 
var parts = str.split("~");
var what_you_want = parts.pop();

Output: eabhyas.blogspot.com


Method 3:
str = "~test content ~thanks ok ~eabhyas.blogspot.com";   
arr = str.split('~');
strFile = arr[arr.length-1];
console.log(strFile );

Output: eabhyas.blogspot.com

Comments