Question: I have multiple lines of text in log files in this kind of format:

```
topic, this is the message part, with, occasional commas.

```

How can I split the string from the first comma so I would have the topic and the rest of the message in two different variables?

I've tried using this kind of split, but it doesn't work when there's more commas in the message part.

```
[topic, message] = whole_message.split(",", 2);

```

Answer: 
javascript's `String.split()` method is broken (at least if you're expecting the same behavior that other language's `split()` methods provide).

An example of this behavior:

```
console.log('a,b,c'.split(',', 2))
> ['a', 'b']

```

and not 

```
> ['a', 'b,c']

```

like you'd expect.

Try this split function instead:

```
function extended_split(str, separator, max) {
    var out = [], 
        index = 0,
        next;

    while (!max || out.length < max - 1 ) { 
        next = str.indexOf(separator, index);
        if (next === -1) {
            break;
        }
        out.push(str.substring(index, next));
        index = next + separator.length;
    }
    out.push(str.substring(index));
    return out;
};  

```
```
let string="topic, this is the message part, with occasional commas."

let arr = new Array(); 
let idx = string.indexOf(',')
arr[0] = string.substring(0, idx);
arr[1] = string.substring(idx+1);
let topic = arr[0];
let message = arr[1]

```

output arr should be: ["topic", "this is the message part, with occasional commas."]