Home > Software engineering >  Is there a way to make .split() skip over certain things in a string?
Is there a way to make .split() skip over certain things in a string?

Time:11-15

I am working on a project where I am sorting Rolling Stone's Top 500 albums by genre. The problem is, I am trying to split all the info (album, band, genre, year) using .split(), but some of the albums have multiple genres. The way the .txt file that I am working with demonstrates multiple genres is by surrounding them with quotation marks, like so: album, band, "genre1, genre2", year. I need to figure out a way to make the function skip over the genres and make the whole genre section one thing.

I haven't really tried much, because I really have no idea what to do. If I seperate by " first, it's still going to split wrong. Here's an example of an album:

Rank, Year, Album, Band, "Genre1, Genre2", Sub-genre 5,1965,Rubber Soul,The Beatles,"Rock, Pop",Pop Rock

There's some code. It doesn't work, but it demonstrates the basic framework. The stuff at the start is just the document reader, because all the stuff is in a .txt file. It starts at function lireInfo(). fichierAlbums is the .txt file. EDIT: I should have made myself a bit more clear. The result would be an array with the following content: 0 is the rank, 1 is the year, 2 is the album, 3 is the band, and 4 is the genre or genres. The genre, even if there is more than one, should be a single element.

Also, this is a school project, so I cannot use jQuery.

 let fichierAlbums = document.getElementById("input-fichier");

        function lireFichier()
        {
            let reader = new FileReader();
            // on va chercher le premier fichier et, une fois
            // téléchargé, on appelle la fonction lireInfo()
            reader.readAsText(fichierAlbums.files[0]);
            reader.onload = lireInfo;
        }

        function lireInfo()
        {
            // le texte du fichier
            let fichierAlbums = this.result;

            let lignesAlbums = fichierAlbums.split("\n");
            
            for (let ligneIndex = 0; ligneIndex < lignesAlbums.length; ligneIndex  )
            {
                let ligneAlbumsTrimmed = lignesAlbums[ligneIndex].trim();
                console.log(ligneAlbumsTrimmed);

CodePudding user response:

You should be using a CSV parser here, with double quotes set as the escape character. That being said, you could parse each row using a CSV trick:

var line = "5,1965,Rubber Soul,The Beatles,\"Rock, Pop\",Pop Rock";
var parts = line.match(/"(.*?)"|[^,] /g)
                .map(x => x.replace(/^"|"$/g, ""));
console.log(parts);

  • Related