Quote (tboogy @ Mar 5 2017 11:06am)
Code
"your string".split(' ').reverse().join(' ');
if the string is longer than 3 words then everything with be in reverse, not just the first and last word
I'd do something like this with a little bit of regex:
Code
var regex = /(^\b\w+\b)(.+)(\b\w+\b$)/;
var result = "your string comes here fish dog cat".replace(regex, '$3$2$1');
1. ( ) brackets represent capturing groups. first group captures the first word, second group captures all the words excluding the first and last word. third group is the last word.
2. in the replace method you swap the first and third group around to get the result you want. $1$2$3 ==> $3$2$1
This post was edited by iasip11 on Mar 5 2017 12:43pm