简体   繁体   中英

Split string based on the first occurring number javascript

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?

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)); 

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(' ', '')); 

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)); 

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);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM