我的html看起来像
<tr>
<td align="left"><a target="_blank" href="/tags/ref_color_tryit.asp?color=Yellow">Yellow</a> </td>
<td align="left"><a target="_blank" href="/tags/ref_color_tryit.asp?hex=FFFF00">#FFFF00</a></td>
<td bgcolor="#FFFF00"> </td>
<td align="left"><a href="/tags/ref_colorpicker.asp?colorhex=FFFF00">Shades</a></td>
<td align="left"><a href="/tags/ref_colormixer.asp?colorbottom=FFFF00&colortop=FFFFFF">Mix</a></td>
</tr>
<tr>
<td align="left"><a target="_blank" href="/tags/ref_color_tryit.asp?color=YellowGreen">YellowGreen</a> </td>
<td align="left"><a target="_blank" href="/tags/ref_color_tryit.asp?hex=9ACD32">#9ACD32</a></td>
<td bgcolor="#9ACD32"> </td>
<td align="left"><a href="/tags/ref_colorpicker.asp?colorhex=9ACD32">Shades</a></td>
<td align="left"><a href="/tags/ref_colormixer.asp?colorbottom=9ACD32&colortop=FFFFFF">Mix</a></td>
</tr>
我想要做的是
过滤html所以我最终只能
<td bgcolor="#XXXXXX"> </td>
然后过滤,最后得到一堆
行XXXXXX
XXXXXX
我该怎么做?
答案 0 :(得分:1)
您好,您可以使用以下正则表达式。
\<td bgcolor\=\"([^\"]+\)">\ \;\<\/td\>
使用群组选项捕获“XXXXXX”
答案 1 :(得分:0)
匹配正确标签的第一个正则表达式:
\<td bgcolor="#[0-9A-Fa-f]{6}"> \</td\>
然后,您可以再次使用(或使用组选项)过滤该数据,具体取决于哪种语言更方便):
[0-9A-Fa-f]{6}
也就是说,如果你想使用正则表达式(不要拍我,问题是我可以使用正则表达式)
答案 2 :(得分:0)
如果你必须使用正则表达式,这是一个使用Ruby的irb的演示:
>> %Q{
<tr>
<td align="left"><a target="_blank" href="/tags/ref_color_tryit.asp?color=Yellow">Yellow</a> </td>
<td align="left"><a target="_blank" href="/tags/ref_color_tryit.asp?hex=FFFF00">#FFFF00</a></td>
<td bgcolor="#FFFF00"> </td>
<td align="left"><a href="/tags/ref_colorpicker.asp?colorhex=FFFF00">Shades</a></td>
<td align="left"><a href="/tags/ref_colormixer.asp?colorbottom=FFFF00&colortop=FFFFFF">Mix</a></td>
</tr>
<tr>
<td align="left"><a target="_blank" href="/tags/ref_color_tryit.asp?color=YellowGreen">YellowGreen</a> </td>
<td align="left"><a target="_blank" href="/tags/ref_color_tryit.asp?hex=9ACD32">#9ACD32</a></td>
<td bgcolor="#9ACD32"> </td>
<td align="left"><a href="/tags/ref_colorpicker.asp?colorhex=9ACD32">Shades</a></td>
<td align="left"><a href="/tags/ref_colormixer.asp?colorbottom=9ACD32&colortop=FFFFFF">Mix</a></td>
</tr>
}.scan(/<td[^>]*> <\/td>/).map {|s| s[/#([a-f0-9]+)/i, 1]}
=> ["FFFF00", "9ACD32"]
答案 3 :(得分:0)
我也不会用正则表达式解析HTML,但如果我这样做,我就会这样做;)
var html = '<tr>\n<td align="left"><a target="_blank" href="/tags/ref_color_tryit.asp?color=Yellow">Yellow</a> </td>\n<td align="left"><a target="_blank" href="/tags/ref_color_tryit.asp?hex=FFFF00">#FFFF00</a></td>\n<td bgcolor="#FFFF00"> </td>\n<td align="left"><a href="/tags/ref_colorpicker.asp?colorhex=FFFF00">Shades</a></td>\n<td align="left"><a href="/tags/ref_colormixer.asp?colorbottom=FFFF00&colortop=FFFFFF">Mix</a></td>\n</tr>\n\n\n<tr>\n<td align="left"><a target="_blank" href="/tags/ref_color_tryit.asp?color=YellowGreen">YellowGreen</a> </td>\n<td align="left"><a target="_blank" href="/tags/ref_color_tryit.asp?hex=9ACD32">#9ACD32</a></td>\n<td bgcolor="#9ACD32"> </td>\n<td align="left"><a href="/tags/ref_colorpicker.asp?colorhex=9ACD32">Shades</a></td>\n<td align="left"><a href="/tags/ref_colormixer.asp?colorbottom=9ACD32&colortop=FFFFFF">Mix</a></td>\n</tr>'
.split('\n'),
colors = [],
i, l,
match;
for(i = 0, l = html.length; i < l; i++) {
if(match = /<td bgcolor="#([\da-fA-F]{6})"> <\/td>/.exec(html[i])) {
colors.push(match[1]);
}
}
console.log(colors);