简体   繁体   English

根据第一个出现的数字分割字符串

[英]Split string based on the first occurring number javascript

I have a string which I want to split into chunks using the .split() function. 我有一个string ,我想使用.split()函数拆分为多个

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 正如OP在评论中所述,预期的输入为99 string

This can be represented in regex as 2 capture groups.. 这可以在正则表达式中表示为2个捕获组。

The first -> \\d\\d 2 numbers.. And the second -> .* anything else.. 第一个-> \\d\\d 2个数字..第二个-> .*其他。

You can then combine this with capture groups.. 然后,您可以将其与捕获组结合。

So the final regex would be /(\\d\\d) (.*)/ 因此最终的正则表达式为/(\\d\\d) (.*)/

If the numbers could be other than just 2 digits long, you might want \\d+ instead. 如果数字的长度可能不是2位数字,则可能需要\\d+

Here is a working example of 99 string 这是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. 如果数字可以是单个,甚至是3,那么可能会更好。

 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. 如果您不希望仅使用split ,则可以将其与slice结合在一起并加入以实现所需的结果。

[
  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. 另一种选择是使用仅与第一个数字匹配的RegExp,但这也需要与filter结合使用,因为它会为输出数组中的第一个条目创建一个匹配项''

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

EDIT: Best final solution: 编辑: 最佳最终解决方案:

To solve with a single RegExp and split you can use the following: 要使用单个RegExp进行splitsplit ,可以使用以下命令:

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" . 此RegExp将拆分(并捕获)从最后一位到第一位之前的整个字符串,即“Samuël1 en 2” This method still produces an excess empty string at the end of the split array which is why the limiter is necessary. 此方法仍在split数组的末尾产生多余的空字符串,这就是为什么需要限制器的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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