Jquery split string and get last item

length - 1]; // Also very fast but it will remove the element from the array also, this may or may not matter in your case. If separator The pop() method removes the last element of an array, and returns that element. Note that the supplied index is zero-based, and refers to the position of elements within the jQuery object, not within the DOM tree. separator, Optional. Dec 26, 2017 The separator is treated as a string or as a regular expression. split(/[\s,]+/);. At this point, pieces is an array and pieces. split('/'). Do you just want the last element? You could do this: var str ="Watch-The-Crap-456"; console. A returned array will be flattened into the resulting array. If separator Definition and Usage. Within the function, this refers to the global (window) object. reverse()[1]; array_fragment isn't a jquery variable, it's a plain old javascript variable so no need for $() Split separates the fragment string where it finds '/'s. slice(str. If there are no commas or spaces it will simply output the string as it was given. The end parameter allows us to limit the selected range even further. If separator is omitted or does not occur in str , the array returned contains one element consisting of the entire string. An integer that specifies the number of splits, items after the split limit will not be included in the array Jun 7, 2016 Need an easy way to get the last element of an array in javascript? Heres a few options: var myArray = [1,2,3,4,5,6]; // Fastest method, requires the array is in a variable myArray[myArray. For example: The function to process each item against. lastIndexOf('-')+1));‚Äč. var str = "hello,how,are,you,today?"; var pieces = str. Note: This method changes the length of an array! Tip: To remove the last item of an array, use the pop() method. If omitted, the entire string will be returned (an array with only one item). If a plain-text separator contains more than one character, that entire string must be found to represent a split point. In this case you must define your string first, which makes sense. length contains the size of the array so to get the last element of the array, you check pieces[pieces. Split takes the The result of this call is a red background for items 3, 4, and 5. The shift() method removes the first item of an array, and returns that item. The first argument to the function is the array item, the second argument is the index in array The function can return any value. You can also think of "second to last element in the array" as "second element of the array reversed": > path = 'a/b/c/d'; > path. length-1] . Parameter, Description. log(str. I don't see any situation where you would want to get the last element from a string literal, you would just write var str = "hello,how,are,you,today?"; var pieces = str. *An array item can be a string, a number, an array, a boolean, or any other object types that are allowed in an array. Definition and Usage. Tip: To remove the first element of an array, use the shift() method. Note: This method changes the length of an array. limit, Optional. Specifies the character, or the regular expression, to use for splitting the string.