-4

Let's say the user gives us a string "12345678" it is easy to separate each digit. If user inputs "899091929394" (2 digit number series) I can't understand how to separate the long string into 2 digit numbers. Further on what if a user inputs 3 digit number series.

My question is: How to detect and separate multi-digit numbers from a long string?

Result should look like Array = [89,90,91,92,93,94]

3 Answers3

1

Here is what you want:

const string = "899091929394";

const splittedString = string.match(/.{1,2}/g);

console.log(splittedString);

OUTPUT:

[ '89', '90', '91', '92', '93', '94' ]

If you want to change the "group by" number, edit the regex /.{1,2}/g.

Dave
  • 1,912
  • 4
  • 16
  • 34
  • 1
    Add the code in snippet that will make answer more interactive and easy to see output. +1 – Code Maniac Mar 05 '19 at 11:19
  • it is not really dynamic. – Nina Scholz Mar 05 '19 at 11:56
  • @NinaScholz what do you mean with dynamic? – Dave Mar 05 '19 at 11:57
  • the question: "*How to detect and separate multi-digit numbers from a long string?*" – Nina Scholz Mar 05 '19 at 11:59
  • @NinaScholz how to descriminate between multi-digit numbers? How do we know if it is 2 digit serie or more? – Dave Mar 05 '19 at 12:01
  • 1
    right, that is the question. i [tried](https://stackoverflow.com/a/55001722/1447675) to spearate the string beginning with a length of one character, check if any predecessor plus one is equal to the actual item. if so, return this array, otherwise increment the part string length. – Nina Scholz Mar 05 '19 at 12:08
1

You could try to separate until you got every item in a series.

function separate(s) {
    var i = 1,
        temp;
    
    while (i < s.length) {
        temp = s.match(new RegExp(`.{${i}}`, 'g')).map(Number);
        if (temp.every((v, i, a) => !i || a[i - 1] + 1 === v || a[i - 1] + 2 === v)) {
            return temp;
        }
        i++;
    }
}


var strings = ["12345678", "8788909192", "899091929394", "234235236237238239"],
    result = strings.map(separate);

result.forEach(a => console.log(...a));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Can you explain what this part of code (v, i, a) => !i || a[i - 1] + 1 === v) is as this function works perfectly but does not complete my assignment as in I need to modify this part. – Ante Dejanovic Mar 05 '19 at 12:52
  • `v` is the value, `i` the index and `a` the array. because you need to chekc two elements, the first element has no predecessor, so `!i` returns `true` for index zero and for all other indices, the second part is evaluated by taking the last element, add one and compare with the actual value. – Nina Scholz Mar 05 '19 at 12:55
  • So if let's say I have to make this work even if numbers are missing in series eg "8788909192" I can just switch .every with .some? – Ante Dejanovic Mar 05 '19 at 13:00
  • no, you need still `every`, but if you have holes of one, then add another check, like `!i || a[i - 1] + 1 === v || a[i - 1] + 2 === v` – Nina Scholz Mar 05 '19 at 13:04
0

function seperateNDigitsFromNumber(numberInput, digitsCount) {
        const regexString = new RegExp("\\d{1,"+digitsCount+"}", "g");
        return numberInput
        .match(regexString)
        .map(stringNum => parseInt(stringNum, 10));
}

seperateNDigitsFromNumber("899091929394",2);
Example : seperateNDigitsFromNumber("899091929394",2) => [89, 90, 91, 92, 93, 94]
satwik
  • 587
  • 5
  • 13