Getting the last element of a split string array
JavascriptSplitJavascript Problem Overview
I need to get the last element of a split array with multiple separators. The separators are commas and space. If there are no separators it should return the original string.
If the string is "how,are you doing, today?" it should return "today?"
If the input were "hello" the output should be "hello".
How can I do this in JavaScript?
Javascript Solutions
Solution 1 - Javascript
There's a one-liner for everything. :)
var output = input.split(/[, ]+/).pop();
Solution 2 - Javascript
const str = "hello,how,are,you,today?"
const pieces = str.split(/[\s,]+/)
const last = pieces[pieces.length - 1]
console.log({last})
At this point, pieces
is an array and pieces.length
contains the size of the array so to get the last element of the array, you check pieces[pieces.length-1]
. If there are no commas or spaces it will simply output the string as it was given.
alert(pieces[pieces.length-1]); // alerts "today?"
Solution 3 - Javascript
var item = "one,two,three";
var lastItem = item.split(",").pop();
console.log(lastItem); // three
Solution 4 - Javascript
Best one ever and my favorite using split
and slice
const str = "hello, how are you today?"
const last = str.split(' ').slice(-1)[0]
console.log({last})
And another one is using split
then pop
the last one.
const str = "hello, how are you today?"
const last = str.split(' ').pop()
console.log({last})
Solution 5 - Javascript
You can also consider to reverse your array and take the first element. That way you don't have to know about the length, but it brings no real benefits and the disadvantage that the reverse operation might take longer with big arrays:
array1.split(",").reverse()[0]
It's easy though, but also modifies the original array in question. That might or might not be a problem.
Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse
Probably you might want to use this though:
array1.split(",").pop()
Solution 6 - Javascript
And if you don't want to construct an array ...
var str = "how,are you doing, today?";
var res = str.replace(/(.*)([, ])([^, ]*$)/,"$3");
The breakdown in english is:
/(anything)(any separator once)(anything that isn't a separator 0 or more times)/
The replace just says replace the entire string with the stuff after the last separator.
So you can see how this can be applied generally. Note the original string is not modified.
Solution 7 - Javascript
There are multiple ways to get last elements
let input='one,two,three,four';
let output1 = input.split(',').pop();
console.log('output1 =',output1);
let split = input.split(',');
let output2 = split[split.length-1];
console.log('output2=',output2);
let output3 = input.split(',').reverse()[0];
console.log('output3=',output3);
let output4 = input.split(',').slice(-1)[0];
console.log('output4=',output4);
Solution 8 - Javascript
This way is easy to understand and very short.
const output = input.split(',').pop().split(' ').pop();
Solution 9 - Javascript
var title = 'fdfdsg dsgdfh dgdh dsgdh tyu hjuk yjuk uyk hjg fhjg hjj tytutdfsf sdgsdg dsfsdgvf dfgfdhdn dfgilkj,n, jhk jsu wheiu sjldsf dfdsf hfdkdjf dfhdfkd hsfd ,dsfk dfjdf ,yier djsgyi kds';
var shortText = $.trim(title).substring(1000, 150).split(" ").slice(0, -1).join(" ") + "...More >>";
Solution 10 - Javascript
You can access the array index directly:
var csv = 'zero,one,two,three'; csv.split(',')[0]; //result: zero csv.split(',')[3]; //result: three