admin管理员组文章数量:1127087
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?
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?
Share Improve this question edited Dec 25, 2015 at 15:10 JJJ 33.2k20 gold badges94 silver badges103 bronze badges asked Mar 16, 2009 at 18:17 solsol 011 Answers
Reset to default 1042There's a one-liner for everything. :)
var output = input.split(/[, ]+/).pop();
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?"
var item = "one,two,three";
var lastItem = item.split(",").pop();
console.log(lastItem); // three
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})
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()
The at()
method takes an integer value and returns the item at that index, allowing for positive and negative integers. Negative integers count back from the last item in the array.
const str = "how,are you doing, today?";
const last = str.split(/[\s,]+/).at(-1);
console.log({last});
Link to documentation: Array.prototype.at()
This way is easy to understand and very short.
const output = input.split(',').pop().split(' ').pop();
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);
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.
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 >>";
You can access the array index directly:
var csv = 'zero,one,two,three';
csv.split(',')[0]; //result: zero
csv.split(',')[3]; //result: three
本文标签: javascriptGetting the last element of a split string arrayStack Overflow
版权声明:本文标题:javascript - Getting the last element of a split string array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736693230a1948045.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论