我想将标题和艺术家与YouTube标题分开。对于前者如果YouTube标题为:Ariana Grande - Problem ft. Iggy Azalea
我想输出某事。像这样:
艺术家:Ariana Grande,Iggy Azalea
标题:问题
我的脚本适用于包含ft, feat or &
但it fails to separate when a title contains more than one separators.
例如,它在此标题Dafina Zeqiri & Ledri Vula ft. Sardi Dj - Got Ur Back
上有效。
它输出Dafina Zeqiri, Ledri Vula
作为艺术家,但不输出Sardi Dj
小提琴:https://jsfiddle.net/2kyLL6j9/5/
我该如何解决这个问题?感谢。
var yt_title, title, artist;
yt_title = 'Ariana Grande - Problem ft. Iggy Azalea'; //this works!
//yt_title = 'capital t feat. 2po2 & lyrical son - facedown';
//yt_title = 'ose Dafina Zeqiri & Ledri Vula ft. Sardi Dj - Got Ur Back';
artist = yt_title.split(" - ")[0].trim();
title = yt_title.split(" - ")[1].trim();
var separators = [" ft. ", " ft ", " feat ", " feat. ", " & "];
//if it has 'ft or other separators' before ( - )
if(title.split(new RegExp(separators.join('|'), 'g'))[1]) {
artist += ', ' + title.split(new RegExp(separators.join('|'), 'g'))[1].trim();
title = title.split(new RegExp(separators.join('|'), 'g'))[0].trim();
}
//if it has 'ft or other separators' after ( - )
if(artist.split(new RegExp(separators.join('|'), 'g'))[1]) {
artist = artist.split(new RegExp(separators.join('|'), 'g'))[0].trim() + ', ' + artist.split(new RegExp(separators.join('|'), 'g'))[1].trim();
}
document.getElementById('title').innerHTML = title;
document.getElementById('artists').innerHTML = artist;
答案 0 :(得分:0)
var yt_title, title, artist;
//yt_title = 'Ariana Grande - Problem ft. Iggy Azalea & lyrical son'; //this works!
yt_title = 'Dafina Zeqiri & Ledri Vula ft. Sardi Dj - Got Ur Back ft. Iggy Azalea & lyrical son';
//yt_title = 'capital t feat. 2po2 & lyrical son - facedown';
//rempve string inside () and [] brackets.
yt_title = yt_title.replace(/[(\[].*?[)\]] */g, '').toLowerCase();
artist = yt_title.split(" - ")[0].trim();
title = yt_title.split(" - ")[1].trim();
var separators = [" ft. ", " ft ", " feat ", " feat. ", " & "];
//if it has 'ft or other separators' before ( - )
var artists = '';
var split_title = title.split(new RegExp(separators.join('|'), 'g'));
for(var i=1; i<split_title.length; i++) {
artists += split_title[i] + ', ';
title = split_title[0];
}
//if it has 'ft or other separators' after ( - )
var split_artists = artist.split(new RegExp(separators.join('|'), 'g'));
for(var i=0; i<split_artists.length; i++) {
artists += split_artists[i] + ', ';
}
document.getElementById('title').innerHTML = title;
document.getElementById('artists').innerHTML = artists;