Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
47
rated 0 times [  54] [ 7]  / answers: 1 / hits: 17668  / 13 Years ago, wed, may 25, 2011, 12:00:00

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

More From » regex

 Answers
41

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



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


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


[#92047] Tuesday, May 24, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
theron

Total Points: 168
Total Questions: 93
Total Answers: 94

Location: South Georgia
Member since Fri, Nov 13, 2020
4 Years ago
;