1

I have a string which I want to split into chunks using the .split() function.

Example strings

59 Samuël 1 en 2 // Example 1
1 Saul omgekomen door het zwaard // Example 2
622 Koningen 1 tot 10 // Example 3

Expected output

['59', 'Samuël 1 en 2'] // Output example 1
['1', 'Saul omgekomen door het zwaard'] // Output example 2
['622', 'Koningen 1 tot 10'] // Output example 3

I tried the following code, based on anwsers found in other topics.

var example_1 = '59 Samuël 1 en 2';
var example_2 = '1 Saul omgekomen door het zwaard';
var example_3 = '622 Koningen 1 tot 10';

console.log(example_1.split(/(\d+)/));
console.log(example_2.split(/(\d+)/));
console.log(example_3.split(/(\d+)/));

But the output is not the expected output, eg. ['59', 'Samuël 1 en 2']

Can someone point me into the right direction?

9
  • In this example you’d be better off splitting on the first whitespace character. Is this example exactly what you want to do or is there a scenario where the first whitespace might be incorrect? Commented Jan 15, 2019 at 10:39
  • @josh I think that might be actualy working. I didn't even think about that :) Commented Jan 15, 2019 at 10:41
  • what is the expected output Commented Jan 15, 2019 at 10:41
  • @brk ['59', 'Samuël 1 en 2'] Commented Jan 15, 2019 at 10:42
  • 1
    In that case you could just do -> "59 Samuël 1 en 2".match(/(\d\d) (.*)/).slice(1) This basically creates 2 capture groups, that I then slice of the first return as that's the original string, the rest are the capture groups your interested in. Commented Jan 15, 2019 at 11:00

5 Answers 5

2

As the OP has stated in comments, the expected input is in 99 string

This can be represented in regex as 2 capture groups..

The first -> \d\d 2 numbers.. And the second -> .* anything else..

You can then combine this with capture groups..

So the final regex would be /(\d\d) (.*)/

If the numbers could be other than just 2 digits long, you might want \d+ instead.

Here is a working example of 99 string

console.log("59 Samuël 1 en 2".match(/(\d\d) (.*)/).slice(1));

If the numbers could be single, or even 3 numbers this might be better.

console.log("59 Samuël 1 en 2".match(/(\d+) (.*)/).slice(1));
console.log("159 Samuël 1 en 2".match(/(\d+) (.*)/).slice(1));
console.log("9 Samuël 1 en 2".match(/(\d+) (.*)/).slice(1));

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, exacly. I changed .match(/(\d\d) (.*)/) into .match(/(\d+) (.*)/) aswell, in my code. And it works as a charme. Thanks, I was breaking my head into solving this :)
YES! I needed to retrieve the first string of numbers (before any non-numeric character) in my string. console.log("59654567,456123".match(/(\d+)/).slice(1)); \\59654567 was great
0

I think using a capturing group with a ".+" to include everything after should work.

var string = '59343 Samuël 1 en 2';
console.log(string.split(/\d+(.+)/)[1].replace(' ', ''));

2 Comments

The output is a string, I want the output to be an array, eg ['59343', 'Samuël 1 en 2']
Oh, shoot sorry I read that too fast. Looks like you got an answer though!
0

You can use capturing parantheses, then filter out the empty matches

const str = '50 Samuël 1 en 2';
console.log(str.split(/(\d+)(.+)/).filter(e => e));

Comments

0

This takes care of it as long as you can count on there being a space after the first series of digits. It's not pretty, but it should be very clear, so improving its efficiency is an option if you prefer.

(Note that splitting on digits removes the digits, which isn't what you're actually looking for according to your expected output.)

var str = '59 Samuël 1 en 2';
var arr = str.split(" ");
let i = 0, firstIntPosInArr = -1;
for(; i < arr.length; i++){
    if(parseInt(arr[i])){
        firstIntPosInArr = i;
        console.log(`${i}: ${arr[i]}`);
        break; 
    }
}

console.log(firstIntPosInArr);

let firstPart = [], secondPart = [], output = [];
for(i = 0; i < arr.length; i++){
    if(i <= firstIntPosInArr){ firstPart.push(arr[i]); }
    else secondPart.push(arr[i]);
}

console.log("1:" + firstPart);
console.log("2:" + secondPart);

output.push(firstPart.join(' '));
output.push(secondPart.join(' '));

console.log(output);

Comments

-1

If you're not tied to using split only you could combine it with slice and join to achieve the desired result.

[
  string.split(/(\d+)/)[0],
  string.split(/(\d+)/).slice(1).join(' ')
]

Another alternative would be to use this RegExp which will only match the first number, but this will also need to be combined with filter as it creates a match for '' as the first entry in the output array.

string.split(/^(\d*) /).filter(Boolean);

EDIT: Best final solution:

To solve with a single RegExp and split you can use the following:

string.split(/[^\d](.*\d*)$/, 2);

This RegExp will split on (and capture) the whole string from the last digit to before the first digit, ie "Samuël 1 en 2". This method still produces an excess empty string at the end of the split array which is why the limiter is necessary.

Comments

Your Answer

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.