从/ r / listenToThis解析歌曲标题以获取IFTTT小程序

我有一系列的歌曲名称,来自这个subreddit,看起来像这样:


[

  "Lophelia -- MYTCH [Acoustic Prog-Rock/Jazz] (2019)",

  "Julia Jacklin - Pressure to Party [Rock] (2019)",

  "The Homeless Gospel Choir - I'm Going Home [Folk-Punk] (2019) cover of Pat the Bunny | A Fistful of Vinyl",

  "Lea Salonga and Simon Bowman - The last night of the world [musical] (1990)",

  "$uicideboy$ - Death",

  "SNFU -- Joni Mitchell Tapes [Punk/Alternative] (1993)",

  "Blab - afdosafhsd (2000)",

  "Something strange and badly formatted without any artist [Classical]",

  "シロとクロ「ミッドナイトにグッドナイト」(Goodnight to Midnight - Shirotokuro) - (Official Music Video) [Indie/Alternative]",

  "Victor Love - Irrationality (feat. Spiritual Front) [Industrial Rock/Cyberpunk]"

  ...

]

我正在尝试从他们那里解析标题和艺术家,但是我真的在正则表达式方面苦苦挣扎。


我尝试使用来拆分它,"-"但是仅在之后获得艺术家真的很烦人。


我也尝试过使用正则表达式,但实际上无法正常工作。这就是我给艺术家的东西:/(?<= -{1,2} )[\S ]*(?= \[|\( )/i 和标题:的东西/[\S ]*(?= -{1,2} )/i。


每个条目都是一首歌的标题。在歌曲标题之前,可能是歌曲的艺术家,然后是一两个(或三个)破折号。然后,可以在方括号中添加类型,在括号中添加发行日期。我不期望完美的准确性,某些格式可能很奇怪,在那种情况下,我宁愿artist是未定义的,而不是一些奇怪的解析。


举个例子:


[

  { title: "MYTCH", artist: "Lophelia" },

  { title: "Pressure to Party", artist: "Julia Jacklin" },

  { title: "I'm Going Home", artist: "The homeless Gospel Choir" },

  { title: "The last night of the world", artist: "Lea Salonga and Simon Bowman" },

  { title: "Death", artist: "$uicideboy$" },

  { title: "Joni Mitchell Tapes", artist: "SNFU" },

  { title: "afdosafhsd", artist: "Blab" },

  { title: "Something strange and badly formatted without any artist" },

  { title: "Goodnight to midnight", artist: "shirotokuro" }, // Probably impossible with some kind of AI

  { title: "Irrationality" artist: "Victor Love" }

]


拉丁的传说
浏览 165回答 2
2回答

慕尼黑5688855

您可以执行以下操作:const songs = [&nbsp; "Lophelia -- MYTCH [Acoustic Prog-Rock/Jazz] (2019)",&nbsp; "Julia Jacklin - Pressure to Party [Rock] (2019)",&nbsp; "The Homeless Gospel Choir - I'm Going Home [Folk-Punk] (2019) cover of Pat the Bunny | A Fistful of Vinyl",&nbsp; "Lea Salonga and Simon Bowman - The last night of the world [musical] (1990)",&nbsp; "Lophelia -- MYTCH [Acoustic Prog-Rock/Jazz]",&nbsp; "Death - $uicideboy$",&nbsp; "SNFU -- Joni Mitchell Tapes [Punk/Alternative] (1993)",&nbsp; "Title - Aritst (2000)",&nbsp; "Something strange and badly formatted without any artist [Classical]",];const trailingRgx = /\s*((\[[^\]]+\])|(\(\d+\))).*$/;const details = songs.map(song => {&nbsp; const splitted = song.split(/\s+\-+\s+/);&nbsp; let title = splitted[0];&nbsp; let artist = splitted[1];&nbsp; if (splitted.length >= 2) {&nbsp; &nbsp; artist = artist.replace(trailingRgx, '');&nbsp; } else {&nbsp; &nbsp; title = title.replace(trailingRgx, '');&nbsp; }&nbsp; return {&nbsp; &nbsp; title,&nbsp; &nbsp; artist&nbsp; }});console.log(details);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript