gpt4 book ai didi

javascript - 将标题和艺术家与 YouTube 标题分开

转载 作者:行者123 更新时间:2023-12-03 10:47:22 25 4
gpt4 key购买 nike

我想将标题和艺术家与 YouTube 标题分开。对于前。如果 YouTube 标题是:Ariana Grande - Problem ft. 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

fiddle :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;

最佳答案

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;

关于javascript - 将标题和艺术家与 YouTube 标题分开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28528923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com