Node js how to parse lines like this?

I want to parse the following lines

3693, Toxic Avenger, (1985), Comedy | Horror

to

3693,
Toxic Avenger, The (1985),
Comedy |. Horror

similarly, the following

161944, The Last Brick in America (2001), Drama

should be analyzed for

161944

The Last Brick in America (2001)

drama

I cannot do this by separating the comma, since there is a comma inside the ",".

Proven solution: LS05 suggested I use a "substring", so I did it and it worked perfectly. here it is.

    var pos1 = line.indexOf(',');
    var line = line.substring(pos1+1); 

    pos1 = line.indexOf(',');
    pos2 = line.lastIndexOf(',');

    let movie_id = line.substring(0,pos1);
    let movie_tag = line.substring(pos1+1,pos2);
    let movie_timespan = line.substring(pos2+1);

Thanks LS05 :)

+4
source share
2 answers

, ,

var str = '3693,"Toxic Avenger, The (1985)",Comedy|Horror';
console.log(str.match(/(".*?"|[^",\s]+)(?=\s*,|\s*$)/g).join("\n"));

( , , )

, , , , , \n

Regex

+6
+2

Source: https://habr.com/ru/post/1670516/


All Articles