d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Javascript Help
Add Reply New Topic New Poll
Member
Posts: 11,254
Joined: Nov 6 2004
Gold: 0.00
Mar 4 2017 11:07am
How can i take a string of words and replace / switch the first and last words with each other?

I.E.
original - this is a sentence
new - sentence is a the
Member
Posts: 36,123
Joined: Jul 18 2008
Gold: 2,407.00
Mar 4 2017 11:30am
use the split() function to split the sentence on white space, switch the first and last elements in the array, then loop over the array and concatenate each word to a new string
Member
Posts: 2
Joined: Mar 5 2017
Gold: 0.00
Mar 5 2017 12:06pm
Code
"your string".split(' ').reverse().join(' ');
Member
Posts: 1,917
Joined: Aug 17 2010
Gold: 280.00
Mar 5 2017 12:36pm
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
Member
Posts: 1,039
Joined: Jul 8 2008
Gold: 1,939.50
Mar 5 2017 04:04pm
Quote (iasip11 @ Mar 5 2017 01:36pm)
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


That regex won't work in a lot of different scenarios.

Try:
^(\W*)(\w+)(.+?)(\w+)(\W*)$

edit: Replace would be $1$4$3$2$5

This post was edited by waraholic on Mar 5 2017 04:11pm
Member
Posts: 1,917
Joined: Aug 17 2010
Gold: 280.00
Mar 5 2017 04:21pm
Quote (waraholic @ Mar 5 2017 03:04pm)
That regex won't work in a lot of different scenarios.

Try:
^(\W*)(\w+)(.+?)(\w+)(\W*)$


edit: nvm, had a brainfar here ==> the third capturing group's ? quantifier isnt necessary because (.+) <== counts whitespace too so you'd always expect one between two words.
and youre right in bringing up the cases with non word chars, but OP doesn't suggest we include them

how do we use your regex though, with the replace?


edit: ok, gotcha. gj covering all possible cases.

This post was edited by iasip11 on Mar 5 2017 04:43pm
Member
Posts: 11,254
Joined: Nov 6 2004
Gold: 0.00
Mar 7 2017 10:51pm
var sent = "programming in javascript sucks"

var arr = sent.split(" ");
var firstWord = arr.shift();
var lastWord = arr.pop();

arr.push(firstWord);
arr.unshift(lastWord)

sent = arr.join (" ");
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll