I have a string like this:
&topic1
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do
eiusmod tempor incididunt ut
&topic2
labore et dolore magna aliqua. Ut enim ad minim
www.example.com/test?id=1&name=abc
veniam, quis nostrud exercitation ullamco lab
&topic3
hello
Each time there is (beginning of line) + & + name + \n
, it should be parsed into a new item.
How is the most natural way to parse it this way with Javascript:
[['topic1', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit,\nsed do\neiusmod tempor incididunt ut'],
['topic2', 'labore et dolore magna aliqua. Ut enim ad minim\nwww.example.com/test?id=1&name=abc\nveniam, quis nostrud exercitation ullamco lab'],
['topic3', 'hello']]
?
I have several problems with this method:
var s = "&topic1\nLorem ipsum dolor sit amet, consectetur adipiscing elit,\nsed do\neiusmod tempor incididunt ut\n\n&topic2\nlabore et dolore magna aliqua. Ut enim ad minim\nwww.example.com/test?id=1&name=abc\nveniam, quis nostrud exercitation ullamco lab\n\n&topic3\nhello";
s.split('&').forEach(function(elt) { console.log(elt.split('\n')[0], elt.split('\n').slice(1)); });
the first item is empty (this can be removed after, but maybe there's a cleaner way?)
if
&
is in the middle of a line (and not beginning) then this code doesn't workI'd like the the text after the header title to be in one single string (e.g.
Lorem ipsum dolor sit amet, consectetur adipiscing elit,\nsed do\neiusmod tempor incididunt ut
), and not split for each\n
How to do a cleaner parsing?