1

我目前正在为包含 ajax 的 XBMC 女巫开发 Web 界面。由于 out ajax 的限制,我被迫使用本地资源,而不是通常生成输出的我的 ajax 类。我有一个字符串。* 表示,此文本可以更改。

这是字符串:

文件名:smb://SERVER/Music/3 Doors Down/2000 The Better Life/07 Better Life.mp3 播放状态:播放歌曲编号:6 类型:音频标题:Better Life 曲目:7 艺术家:3 Doors Down 专辑:The Better Life类型:另类年份:2000 URL:smb://xbox:xbox@SERVER/Music/3 Doors Down/2000 The Better Life/07 Better Life.mp3 歌词:比特率:193 采样率:44 Thumb:DefaultAlbumCover.png 时间:02 :05 持续时间:03:07 百分比:66 文件大小:4509417 更改:假

我想知道如何匹配 Title、Arist、Time 和 Duration。我尝试使用正则表达式,但没有成功,因为我没有太多的 JS 知识。

谢谢,布兰特杰

编辑:“你确定那是字符串吗?所有这些都像那样运行,没有换行符?编辑:我编辑了问题以修复格式。 - Ariel 2 小时前”

不,来自http://xbox/xbmcCmds/xbmcHttp?command=GetCurrentlyPlaying的输出 在播放视频时看起来像这样

HTML 代码:

<html> 
<li>Filename:smb://SERVER/Movies/Drive Angry/Drive Angry (2011) DVDRip XviD-MAXSPEED.avi
<li>PlayStatus:Playing
<li>VideoNo:0
<li>Type:Video
<li>Thumb:DefaultVideoCover.png
<li>Time:00:00:28
<li>Duration:01:44:31
<li>Percentage:0
<li>File size:1666804442
<li>Changed:False</html> 

播放音乐时,它有点不同。

<html> 
<li>Filename:smb://SERVER/Music/3 Doors Down/2000 The Better Life/01 Kryptonite.mp3
<li>PlayStatus:Playing
<li>SongNo:-1
<li>Type:Audio
<li>Title:Kryptonite
<li>Track:1
<li>Artist:3 Doors Down
<li>Album:The Better Life
<li>Genre:Alternative
<li>Year:2000
<li>URL:smb://xbox:xbox@SERVER/Music/3 Doors Down/2000 The Better Life/01 Kryptonite.mp3
<li>Lyrics:
<li>Bitrate:192
<li>Samplerate:44
<li>Thumb:DefaultAlbumCover.png
<li>Time:00:05
<li>Duration:03:54
<li>Percentage:2
<li>File size:5618471
<li>Changed:False</html> 
4

2 回答 2

1

假设以下字符串:

var str = "Filename:smb://SERVER/Music/3 Doors Down/2000 The Better Life/07 Better Life.mp3 PlayStatus:Playing SongNo:6 Type:Audio Title:Better Life Track:7 Artist:3 Doors Down Album:The Better Life Genre:Alternative Year:2000 URL:smb://xbox:xbox@SERVER/Music/3 Doors Down/2000 The Better Life/07 Better Life.mp3 Lyrics: Bitrate:193 Samplerate:44 Thumb:DefaultAlbumCover.png Time:02:05 Duration:03:07 Percentage:66 File size:4509417 Changed:False";

将属性提取到字典/地图中:

var dict = (" " + str).split(/ (\w+):/).reduce(function(acc, el, i, orig) {
    if (i % 2)
        acc[el] = orig[i + 1];
    return acc;
}, {});

这是没有高阶函数的同一件事:

var i, dict = {}, pair = (" " + str).split(/ (\w+):/);
for (i = 1; i < pair.length; i += 2)
    dict[pair[i]] = pair[i + 1];

检索值现在很简单:

console.log(dict["Title"]);
console.log(dict["Artist"]);
console.log(dict["Time"]);
console.log(dict["Duration"]);

输出:

Better Life
3 Doors Down
02:05
03:07
于 2011-07-19T21:58:52.650 回答
0

这是我使用正则表达式的方法:

var data = "Filename:smb://SERVER/Music/3 Doors Down/2000 The Better Life/07 Better Life.mp3 PlayStatus:Playing SongNo:6 Type:Audio Title:Better Life Track:7 Artist:3 Doors Down Album:The Better Life Genre:Alternative Year:2000 URL:smb://xbox:xbox@SERVER/Music/3 Doors Down/2000 The Better Life/07 Better Life.mp3 Lyrics: Bitrate:193 Samplerate:44 Thumb:DefaultAlbumCover.png Time:02:05 Duration:03:07 Percentage:66 File size:4509417 Changed:False";

function matchField(field, str) {
    // regexp: space, field, colon, (fewest number of chars), space, fewest number of non-whitespace chars, colon
    var re = new RegExp("\\s" + field + ":(.+?)\\s\\S+?:");
    try {
        return(str.match(re)[1]);
    } catch(e) {}
    return("");
}

var songTitle = matchField("Title", data);
var artist = matchField("Artist", data);
var duration = matchField("Duration", data);

你可以在这里看到它的实际效果:http: //jsfiddle.net/jfriend00/Y9g7x/

结果:

Title: Better Life
Artist: 3 Doors Down
Duration: 03:07

我认为实际上可以使用一个正则表达式完成所有操作,但这会迫使不同字段的顺序每次都完全相同。

于 2011-07-19T22:16:56.487 回答