Splitting string from the first occurrence of a character
Asked Answered
P

8

8

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);
Plumbo answered 25/5, 2011 at 21:30 Comment(5)
Why doesn't split(",", 2) work for you?Amethyst
@Amethyst probably because that assignment statement is totally bogus :-)Attachment
JavaScript != Python :-)Winstead
@Amethyst because if the whole message goes like "mighty topic, hi, I'm an user.", the topic variable would contain "mighty topic" and the message would contain only "hi" instead of "hi, I'm an user."Plumbo
@Martijn: unless you are using a Javascript 1.7+ interpreter, CoffeScript or a Harmony interpreter, where destructuring assignment is available :) (and other Python-like features too) -- this might be what confused the OP.Facesaving
C
14

Use a regex that gets "everything but the first comma". So:

whole_message.match(/([^,]*),(.*)/)

[1] will be the topic, [2] will be the message.

Claxton answered 25/5, 2011 at 21:34 Comment(5)
Note that . doesn't match \n.Dorathydorca
use the m flag to match newlines whole_message.match(/([^,]*),(.*)/m);Landing
if you want newlines, use /([^,]*),([\s\S]*)/mClaxton
@digitalbath: /./ will never match newlines in JSClaxton
/m doesn't change how . works, only /s does. JavaScript has no modifier for that. You have to use something like [\s\S] instead. Eg: /^([^,]*),([\s\S]*)/Mcilwain
A
4

That sort of decomposing assignment doesn't work in Javascript (at the present time). Try this:

var split = whole_message.split(',', 2);
var topic = split[0], message = split[1];

edit — ok so "split()" is kind-of broken; try this:

var topic, message;
whole_message.replace(/^([^,]*)(?:,(.*))?$/, function(_, t, m) {
  topic = t; message = m;
});
Attachment answered 25/5, 2011 at 21:32 Comment(3)
You are ignoring the fact that the data can contain commasSateia
the result seems to be the same as with my splitting: message part is also cut apart if there's more commas present.Plumbo
That's because String.split() in javascript is slightly broken (or at least its behavior is different from all other languages I've used)Landing
S
4

Here!

String.prototype.mySplit = function(char) { 
  var arr = new Array(); 
  arr[0] = this.substring(0, this.indexOf(char)); 
  arr[1] = this.substring(this.indexOf(char) + 1); 
  return arr; 
}

str = 'topic, this is the message part, with, occasional commas.'
str.mySplit(',');
-> ["topic", " this is the message part, with, occasional commas."]
Sateia answered 25/5, 2011 at 21:34 Comment(3)
Modifying the prototype object of JavaScript built-ins can have unexpected and undesired side effects. I'd stay away from this pattern.Landing
I wouldn't modify the global string prototype for a small, single purpose thing like this. Maybe if this was something you were going to use really frequently and in an environment where you don't need to worry about namespace collisions...Claxton
I didn't really intend to cause this kind of outcry, this could easily be a basic function with two parameters. I only used the prototype because it was on my brain from a previous project.Sateia
L
3

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;
};  
Landing answered 25/5, 2011 at 21:37 Comment(0)
J
2
var a = whole_message.split(",");
var topic = a.splice (0,1);

(unless you like doing things complicated ways)

Jig answered 25/5, 2011 at 21:51 Comment(0)
A
0

Why not split by comma, take the [0] item as topic then remove the topic(+,) from the original string ?

You could:

var topic = whole_message.split(",")[0]

(using prototype.js)

var message = whole_message.gsub(topic+", ", "") 

(using jQuery)

whole_message.replace(topic+", ", "")

Or quicker, go with josh.trow

Anaphylaxis answered 25/5, 2011 at 21:36 Comment(0)
M
0
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."]

Mormon answered 12/7, 2022 at 22:33 Comment(0)
B
0

Split using /,(.*)/ or rather /, *(.*)/ to account for the space after the comma

Example:

const str = "topic, this is, the, message."
const [topic, message] = str.split(/, *(.*)/);

console.log(topic);   // "topic"
console.log(message); // "this is, the, message."
Baseless answered 11/9 at 11:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.