Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
68
rated 0 times [  75] [ 7]  / answers: 1 / hits: 29979  / 11 Years ago, sat, june 15, 2013, 12:00:00

How can we get the last word from a string using JavaScript / jQuery?



In the following scenario the last word is Collar. The words are separated by -.



Closed-Flat-Knit-Collar
Flat-Woven-Collar
Fabric-Collar
Fabric-Closed-Flat-Knit-Collar

More From » jquery

 Answers
0

Why must everything be in jQuery?



var lastword = yourString.split(-).pop();


This will split your string into the individual components (for exampe, Closed, Flat, Knit, Collar). Then it will pop off the last element of the array and return it. In all of the examples you gave, this is Collar.


[#77614] Thursday, June 13, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jimmieo

Total Points: 515
Total Questions: 102
Total Answers: 110

Location: Kazakhstan
Member since Mon, Sep 26, 2022
2 Years ago
;