Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将链接插入列表的Javascript reg exp_Javascript_Jquery_Html_Regex - Fatal编程技术网

将链接插入列表的Javascript reg exp

将链接插入列表的Javascript reg exp,javascript,jquery,html,regex,Javascript,Jquery,Html,Regex,我在我的网站上有一堆跟踪列表内容,其格式如下: <div class="tracklist"> 1. Artist - Title (Record Label) 2. Another artist - Title (Another label) </div> 使用jQuery查找匹配的字符串: $(".tracklist").html().match(label) $(".tracklist").html().match(artist) 然后使用substring()

我在我的网站上有一堆跟踪列表内容,其格式如下:

<div class="tracklist">
1. Artist - Title (Record Label)
2. Another artist - Title (Another label)
</div>
使用jQuery查找匹配的字符串:

$(".tracklist").html().match(label)
$(".tracklist").html().match(artist)
然后使用
substring()
方法删除数字、句点、空格、破折号和括号。但是,插入链接并保留文本的好方法是什么呢


在更一般的层面上,这个想法是可行的还是属于“不要用JavaScript解析HTML”的范畴?服务器端实现是否更可取(使用一些XML/XSL魔法)

服务器端实现肯定会更好。你从哪里获取下面的数据?你肯定有数组或类似的信息吗

1. Artist - Title (Record Label)
2. Another artist - Title (Another label)

此外,如果用户没有javascript(现在几乎可以忽略不计,但确实发生了!),服务器端也会贬值。

它不属于“不使用..解析html”的范畴,因为您不是在解析html,而是在解析文本并从中创建html

您可以获得div的全部文本内容:

var text = $('.tracklist').text();
然后分成几行:

var lines = text.split(/\r?\n/);
并分别分析每一行:

function parseLine(line) {
    var match = line.match(/^\d+\.\s+([^-]+)\s-\s([^(]+)(\s*(.*))/);
    if (match) {

        var artist = match[1], title = match[2], label = match[4];

        // create HTML here
    }       
}

$.each(lines, function(index, line) {
    var elems = parseLine(line);
    // append elems to the div
}
正则表达式可以解释如下:

/^\d+\. # this matches the number followed by the dot at the begining
\s+     # the number is separated by one or more whitespace
([^-]+) # the artist: match everything except "-"
\s-\s   # matches the "-" separated by one or more whitespace
([^(]+) # the title: matches everything except "("
(\s+    # one or more whitespace
(.*))/  # the label

我认为切换到XSLT没有任何意义,因为您仍然必须将DIV的内容作为文本处理。对于这种情况,jQuery/regex几乎是最好的了。您只是没有尽可能有效地使用正则表达式。正如@arnaud所说,您应该一次匹配并处理一整行,使用捕获组来分解有趣的部分。下面是我将使用的正则表达式:

/^(\d+)\.\s*([^-]+?)\s*-\s*([^(]+?)\s*\((.*)\)/
match[1]
是曲目编号,
match[2]
是艺术家,
match[3]
是标题,而
match[4]
是标签


我还对它进行了安排,这样就不会捕获周围的空白或其他字符——事实上,大部分空白都是可选的。根据我的经验,像这样的格式化数据通常包含不一致的间距;这使得正则表达式更有可能匹配您希望它匹配的内容,并赋予您纠正不一致的能力。(当然,它也可能包含更严重的缺陷,但这些缺陷通常必须根据具体情况进行处理。)

网站上的信息都是文本格式的,因此我想我也必须在那里使用某种reg exp。说到xml/xsl,我也是一个新手(javascript也是新手,但不太熟悉)。但是也许我应该更仔细地研究一下。多谢阿诺,这看起来可能很有效,我会尝试一下。+1,但您似乎忘记了将
(唱片标签)
的括号转义。不,这实际上是捕获括号(它们也捕获了标签的括号)但是,行分割模式似乎对我不起作用,在html中,行是由
分隔的。也许这与jquery text()函数有关?
/^\d+\. # this matches the number followed by the dot at the begining
\s+     # the number is separated by one or more whitespace
([^-]+) # the artist: match everything except "-"
\s-\s   # matches the "-" separated by one or more whitespace
([^(]+) # the title: matches everything except "("
(\s+    # one or more whitespace
(.*))/  # the label
/^(\d+)\.\s*([^-]+?)\s*-\s*([^(]+?)\s*\((.*)\)/