I have this regexp working, simple, but I feel like it may not be the best way to code it. Basically, I have a playlist separated by line breaks returned as tcp data like so:
file: http://192.168.100.214/Music/justchill.mp3
Artist: Brian G
Title: Just Chill
Track: 1
Date: 2014
Genre: Instrumental
Pos: 0
Id: 0
file: http://192.168.100.214/Music/justchill.mp3
Pos: 1
Id: 1
file: http://streaming.radionomy.com/ABC-Lounge
Pos: 2
Id: 2
OK
Now you can see the fields aren't always the same, but they always start with file and have the pos and id, always ending with id. I'm, parsing this data by line break to create an object, and then slicing it out of the string. Then I run the regexp on the new string, repeat while checking for Id: If it finds Id, I create a new object and start the whole process over.
It works fine, but I don't know, just feels like there may be a more efficient way to do it.
var parseplaylist = function(data)
{
var pattern = /(.+): (.+)\n/;
var result = pattern.exec(data);
var playlistarray = [];
if (result !== null)
{
var playlistobject = {};
while (result !== null)
{
playlistobject[result[1]] = result[2];
if (result[1] == "Id")
{
playlistarray[playlistobject.Pos] = playlistobject;
playlistobject = {};
}
data = data.substr(result[0].length);
result = pattern.exec(data);
}
}
return playlistarray;
};