Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript regular expression - multiple line

I have a file with following text structure and would like to parse date inside into an array ...

21/5/12 14:23:36: A: XXXX
YYY
ZZZ

21/5/12 14:23:25: B: XXX ZZZ YYY

21/5/12 14:23:25: B: XXX ZZZ YYY

I am using data.match(/[^\r\n]+\d+.*/g) to parse data from file and the result is

arr[0], 21/5/12 14:23:36: A: XXXX
arr[1], 21/5/12 14:23:25: B: XXX ZZZ YYY
arr[2], 21/5/12 14:23:25: B: XXX ZZZ YYY

Some text of the first item has been removed which is not desired.

Is it possible to use regular expression to parse the text like this?

like image 910
user3155956 Avatar asked Mar 12 '26 13:03

user3155956


1 Answers

I'm not sure the exact requirement. But if there's empty line between each data item, you can do it like this:

var data ="21/5/12 14:23:36: A: XXXX\r\nYYY\nZZZ\r\n\r\n21/5/12 14:23:25: B: XXX ZZZ YYY\r\n\r\n21/5/12 14:23:25: B: XXX ZZZ YYY";
data.split(/\r\n\r\n/);

Result of this code is:

["21/5/12 14:23:36: A: XXXX
YYY
ZZZ", "21/5/12 14:23:25: B: XXX ZZZ YYY", "21/5/12 14:23:25: B: XXX ZZZ YYY"]
like image 97
ntalbs Avatar answered Mar 15 '26 03:03

ntalbs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!