从字符串 [Nodejs] 中提取 img 标签源



我有一个看起来像这样的字符串

<table border="0" cellpadding="2" cellspacing="3"><tr><td><img src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRO8iLLBmxFL2lvSfboTwwmH3yGF12PdsJe56rTAzJtbsFfS07I1YM_ZzavbwJREe7bUmhFR3ATyA" border="1"></td><td><ol style="list-style: none; margin: 0; padding: 0;"><strong><li><a href="http://newsday.co.tt/2017/11/14/ansa-mcal-sends-5-containers-of-relief-items/" target="_blank">ANSA McAl sends 5 containers of relief items</a>&nbsp;&nbsp;<font color="#6f6f6f">Trinidad News</font></li></strong><a href="https://news.google.com/story/?hl=en&ned=us" target="_blank">Full coverage</a></ol></td></tr></table>

我想做的是提取图像标签的来源。关于从哪里开始完成此操作的任何建议?

您可以使用

.split() ,这在尝试从字符串中提取相关数据时非常有用:

'<table border="0" cellpadding="2" cellspacing="3"><tr><td><img src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRO8iLLBmxFL2lvSfboTwwmH3yGF12PdsJe56rTAzJtbsFfS07I1YM_ZzavbwJREe7bUmhFR3ATyA" border="1"></td><td><ol style="list-style: none; margin: 0; padding: 0;"><strong><li><a href="http://newsday.co.tt/2017/11/14/ansa-mcal-sends-5-containers-of-relief-items/" target="_blank">ANSA McAl sends 5 containers of relief items</a>&nbsp;&nbsp;<font color="#6f6f6f">Trinidad News</font></li></strong><a href="https://news.google.com/story/?hl=en&ned=us" target="_blank">Full coverage</a></ol></td></tr></table>'
  .split('src="')[1]
  .split('"')[0]

从本质上讲,这将执行以下操作:

  1. 根据子字符串src="将字符串拆分为数组
  2. 根据
  3. 子字符串"将该数组中的第二项拆分为新数组
  4. 返回该数组中的第一个元素。

当然还有其他方法可以做到这一点(例如,正则表达式(。

最新更新