I have this string
var str = "394987011016097814 1d the quick brown fox jumped over the lazy dog";
..and i'm trying to get it to become this array
[
"394987011016097814",
"1d",
"the quick brown fox jumped over the lazy fox",
]
I've seen this answer Split string on the first white space occurrence but that's only for first space.
asked Mar 1, 2019 at 16:33
Jeremy John
1,7253 gold badges18 silver badges32 bronze badges
-
2There's no black magic required to get the output as requested. So what have you tried so far and why isn't that (or one of the many answers with some minor adjustments from the linked question) already the answer to your request?Andreas– Andreas2019年03月01日 16:36:46 +00:00Commented Mar 1, 2019 at 16:36
-
are you sure you want the whitespace at the end of the first element in the resulting array?manonthemat– manonthemat2019年03月01日 16:42:49 +00:00Commented Mar 1, 2019 at 16:42
-
1By the way, it's "The quick brown fox jumps over the lazy dog." ;)Scott Marcus– Scott Marcus2019年03月01日 16:43:37 +00:00Commented Mar 1, 2019 at 16:43
6 Answers 6
Destructure using split and join
var str = "394987011016097814 1d the quick brown fox jumped over the lazy fox";
var [str1, str2, ...str3] = str.split(' ');
str3 = str3.join(' ');
console.log([str1, str2, str3])
answered Mar 1, 2019 at 16:39
ellipsis
12.2k2 gold badges19 silver badges33 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Andreas
@JeremyJohn So the accepted answer is a variation of one of the answers from the linked question?
Jeremy John
@Andreas which answer in the linked question? I don't see it.
Source: Split a string only the at the first n occurrences of a delimiter
var string = 'Split this, but not this',
arr = string.split(' '),
result = arr.splice(0,2);
result.push(arr.join(' ')); // result is ["Split", "this,", "but not this"]
alert(result);
answered Mar 1, 2019 at 16:43
Sanasol
9121 gold badge8 silver badges24 bronze badges
Comments
You can split first on all the space and than take the two values and join the remaining values.
var str = "394987011016097814 1d the quick brown fox jumped over the lazy fox";
let op = str.split(/[ ]+/g)
let final = [...op.splice(0,2), op.join(' ')]
console.log(final)
answered Mar 1, 2019 at 16:43
Code Maniac
37.9k5 gold badges44 silver badges65 bronze badges
Comments
You can achieve this by writing some code:
const str = "394987011016097814 1d the quick brown fox jumped over the lazy fox";
const splittedString = str.split(' ');
let resultArray = [];
let concatenedString = '';
for (let i = 0; i < splittedString.length; i++) {
const element = splittedString[i];
if (i === 0 || i === 1) {
resultArray.push(element);
} else {
concatenedString += element + ' ';
}
}
resultArray.push(concatenedString.substring(0, concatenedString.length - 1));
console.log(resultArray);
// output is:
// [ '394987011016097814',
// '1d',
// 'the quick brown fox jumped over the lazy fox' ]
answered Mar 1, 2019 at 16:39
Dave
2,2704 gold badges20 silver badges40 bronze badges
Comments
let str = "394987011016097814 1d the quick brown fox jumped over the lazy fox";
let arr = [];
str = str.split(' ');
arr.push(str.shift());
arr.push(str.shift());
arr.push(str.join(' '));
console.log(arr);
answered Mar 1, 2019 at 16:39
Nafis Islam
1,5172 gold badges16 silver badges34 bronze badges
Comments
use regex ^(\d+)\s(\S+)\s(.*) this way
var re = new RegExp(/^(\d+)\s(\S+)\s(.*)/, 'gi');
re.exec('394987011016097814 1d the quick brown fox jumped over the lazy fox');
var re = new RegExp(/^(\d+)\s(\S+)\s(.*)/, 'g');
var [, g1, g2, g3] = re.exec('394987011016097814 1d the quick brown fox jumped over the lazy fox');
console.log([g1, g2, g3]);
The fourth bird
165k16 gold badges61 silver badges75 bronze badges
Comments
lang-js