I'm using an API which returns text in the following format:
#start
#p 12345 foo
#p 12346 bar
#end
#start
#p 12345 foo2
#p 12346 bar2
#end
My parsing function:
function parseApiResponse(data) {
var results = [], match, obj;
while (match = CST.REGEX.POST.exec(/(#start)|(#end)|#p\s+(\S+)\s+(\S+)/ig)) {
if (match[1]) { // #start
obj = {};
} else if (match[2]) { // #end
results.push(obj);
obj = null; // prevent accidental reuse
// if input is malformed
} else { // #p something something
obj[match[3]] = match[4];
}
}
return results;
}
This will give me a list of objects which looks something like this:
[{ '12345': 'foo', '12346': 'bar'}, /* etc... */]
However, if a line is formatted like this
#start
#p 12345
#p 12346 bar
#end
The line would actually be #p 12345\n and my match[4] would contain the next row's #p.
How do I adjust the pattern to adapt to this?
\S+. Maybe that will give you a hint.