有没有一种方法可以从两个标签之间提取文本,并以我选择的格式输出?是否有任何应用程序或脚本可以执行此操作?
例如:
[b]1.[/b] [artist]Norman Bass[/artist] – How U Like Bass? (Warp Brothers Club Mix) [i](3:26)[/i]
[b]2.[/b] [artist]Rhythmkillaz[/artist] – Wack Ass M.F. (Payback Mix - Clean Version) [i](3:23)[/i]
[b]3.[/b] [artist]Mario Piu'[/artist] & [artist]DJ Arabesque[/artist] – The Vision (Vision 1 Mix) [i](3:41)[/i]
[b]4.[/b] [artist]Cosmic Gate[/artist] – Fire Wire (Club Mix) [i](2:47)[/i]
[b]5.[/b] [artist]Push[/artist] – Strange World (2000 Remake) [i](6:02)[/i]
[b]6.[/b] [artist]Cortina[/artist] – Music Is Moving (Kumara Mix) [i](3:40)[/i]
[b]7.[/b] [artist]Public Domain[/artist] – Operation Blade (Original Mix) [i](3:44)[/i]
我想提取 [artist] 标签之间的信息,并将其输出为一行,条目之间用逗号分隔。
有什么东西可以做到这一点吗?
最佳答案
在 php 中(使用正则表达式):
<?php
function everything_in_tags($string, $tagname)
{
$pattern = "#\[\s*?$tagname\b[^>]*\](.*?)\[/$tagname\b[^>]*\]#s";
preg_match($pattern, $string, $matches);
return $matches[1];
}
?>
这里我们指定标记名并获取数据 b/w。
引用:get everthing between and with php
在 jQuery 中:
var regExp = /\]([^\[]+)\[/;
var matches = regExp.exec("[article]I expect five hundred dollars ($500).[/article]");
//matches[1] contains the value between the parentheses
console.log(matches[1]);
这匹配了 ]
和 [
之间的任何内容,并将其记录在控制台上。
这里我们在字符串中搜索除 [
之外的任何内容。
jQuery 解决方案:fiddle
关于javascript - 有没有办法从两个标签之间提取文本,并以我选择的格式输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37097884/