0

I have an textfile with this structure:

06-05-13 21:52:10: Wence: Hey how you're doing?

10-05-13 16:14:49: Wence: Everythings okay over here

10-05-13 16:14:52: Wence: Nevermind, I just spoke to your mom and she told me you are fine.

10-05-13 16:14:58: Wence: I'll host the bbq tonight. Would be fun if you came.

From this large string, I need to read the date, time, and message, but I need it in a key/value pair like this:

var message = {"Wence": "Hey how you're doing?", "Wence": "Everythings okay over here"}
var time = {"Wence": "16:14:52", "Wence": "16:14:49"}
var date = {"Wence": "10-05-13", "Wence": "10-05-13"}

I am using these RegEx patterns to find the message, date and time:

Date: /(\d{2})-(\d{2})-(\d{2})/
Time: /(\d{2}):(\d{2}):(\d{2})/
Name: /[A-z][0-9]\:/
Message: (need to figure this out, don't know how to search for a string between patterns)

Strangely enough, when I'm trying to split these regex function like

var date = /(\d{2})-(\d{2})-(\d{2})/; 
var dates = chat.split(date);

It doens't split the date in a way that I can save it in an Array.

I splits it like "xx","xx","xx","rest of string","xx","xx","xx

Any help on how to solve this problem and how to parse the message is highly appriciated.

1 Answer 1

4

Try parsing the entire line:

var regex = /(\d\d-\d\d-\d\d) (\d\d:\d\d:\d\d): ([^:]+): (.*)/g;
var messages = [];
var match;
while( match = regex.exec(chat)) {
    messages.push({
        date: match[1],
        time: match[2],
        name: match[3],
        message: match[4]
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

Please don't Doge-compliment me again XD

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.