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.

Jeremy John
  • 1,665
  • 3
  • 18
  • 31
  • 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? – Andreas Mar 01 '19 at 16:36
  • are you sure you want the whitespace at the end of the first element in the resulting array? – manonthemat Mar 01 '19 at 16:42
  • 1
    By the way, it's "The quick brown fox jumps over the lazy dog." ;) – Scott Marcus Mar 01 '19 at 16:43

6 Answers6

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])
ellipsis
  • 12,049
  • 2
  • 17
  • 33
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);
Sanasol
  • 892
  • 8
  • 24
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)
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
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
  • 154,723
  • 16
  • 55
  • 70
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' ]
Dave
  • 1,912
  • 4
  • 16
  • 34
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);
Nafis Islam
  • 1,483
  • 1
  • 14
  • 34