2

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
3
  • 2
    There'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? Commented Mar 1, 2019 at 16:36
  • are you sure you want the whitespace at the end of the first element in the resulting array? Commented Mar 1, 2019 at 16:42
  • 1
    By the way, it's "The quick brown fox jumps over the lazy dog." ;) Commented Mar 1, 2019 at 16:43

6 Answers 6

5

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
Sign up to request clarification or add additional context in comments.

2 Comments

@JeremyJohn So the accepted answer is a variation of one of the answers from the linked question?
@Andreas which answer in the linked question? I don't see it.
3

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

Comments

1

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

Comments

0

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

Comments

0

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

Comments

0

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
answered Mar 1, 2019 at 16:39

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.