-1

I am trying to parse an itunes XML library. I want to get an array of all of the unique artist names from the XML file. I already tried converting it to JSON, but the way that itunes stores their library in XML made it incredibly difficult to access all of the artist names in the library. The regex seeemed much more effective for this purpose.

The format of the file is like this:

<dict>
    <key>Track ID</key><integer>219</integer>
    <key>Name</key><string>Something Sweet, Something Tender</string>
    <key>Artist</key><string>Eric Dolphy</string>
    <key>Album Artist</key><string>Eric Dolphy</string>
    <key>Album</key><string>Out to Lunch (Remastered)</string>
    <key>Genre</key><string>Jazz</string>
    <key>Kind</key><string>Purchased AAC audio file</string>
    <key>Size</key><integer>12175953</integer>
    <key>Total Time</key><integer>363949</integer>
    <key>Disc Number</key><integer>1</integer>
    <key>Disc Count</key><integer>1</integer>
    <key>Track Number</key><integer>2</integer>
    <key>Track Count</key><integer>5</integer>
    <key>Year</key><integer>1964</integer>
    <key>Date Modified</key><date>2016-04-29T09:36:10Z</date>
    <key>Date Added</key><date>2007-08-04T16:57:47Z</date>
    <key>Bit Rate</key><integer>256</integer>
    <key>Sample Rate</key><integer>44100</integer>
    <key>Release Date</key><date>1964-02-25T00:00:00Z</date>
    <key>Artwork Count</key><integer>1</integer>
    <key>Sort Album</key><string>Out to Lunch</string>
    <key>Sort Artist</key><string>Eric Dolphy</string>
    <key>Sort Name</key><string>Something Sweet, Something Tender</string>
    <key>Persistent ID</key><string>4AE13A27A2113C97</string>
    <key>Track Type</key><string>Remote</string>
    <key>Purchased</key><true/>
</dict>

I have an xml file, which can contain hundreds of different artists. "Data" is the contents of the xml file of which the above XML example is just one track.

I am using regex and string.match() to match: <key>Artist</key><string>Eric Dolphy</string> and return the artist's name. It returns an array of all of the matches, but I only want the artist name not the xml tags. I have found that using string.match() with regex /g in javascript returns an array containing all of the matched substrings, but the capture groups are not returned. Is there a way in javascript that I can get an array returned of just the artist names without using str.replace() to replace everything I don't want with an empty string afterwards?

let artists = data.toString().match(/<key>Artist<\/key><string>(.*?)<\/string>/g);
let uniqueArtists = Array.from(new Set(artists))

3
  • Use lookahead and lookbehind. Commented Sep 21, 2016 at 19:30
  • 1
    What is data? How did you create that object? Commented Sep 21, 2016 at 19:33
  • 1
    Never parse XML using regex. Commented Sep 21, 2016 at 21:02

1 Answer 1

-1

Match returns an array of matches of your regex. The first item in the array will be the complete match, the second item will match your first sub pattern (between parentheses). etc..

What you're looking for is:

let artist = artists[1];

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.