如何提取数组中的对象的一部分

时间:2015-07-13 14:21:16

标签: javascript arrays object

我想从以下每个数组对象中提取datetime的值:

[<time pubdate class="dt-updated" datetime="2015-07-09T11:50:32+0000" title="Time posted: 09 Jul 2015, 11:50:32 (UTC)" aria-label="Posted on 09 Jul">09 Jul</time>, <time pubdate class="dt-updated" datetime="2015-06-18T13:59:49+0000" title="Time posted: 18 Jun 2015, 13:59:49 (UTC)" aria-label="Posted on 18 Jun">18 Jun</time>, <time pubdate class="dt-updated" datetime="2015-06-18T12:56:47+0000" title="Time posted: 18 Jun 2015, 12:56:47 (UTC)" aria-label="Posted on 18 Jun">18 Jun</time>, <time pubdate class="dt-updated" datetime="2015-06-18T12:41:42+0000" title="Time posted: 18 Jun 2015, 12:41:42 (UTC)" aria-label="Posted on 18 Jun">18 Jun</time>]

有没有人知道是否有一个数组对象的方法可以帮助我在创建循环时执行此操作?我在网上搜索到目前为止还没找到任何东西......

1 个答案:

答案 0 :(得分:1)

我使用正则表达式匹配datetime="(.*?)"

really need

这会将所有日期时间值放入数组中:

var str = '[<time pubdate class="dt-updated" datetime="2015-07-09T11:50:32+0000" title="Time posted: 09 Jul 2015, 11:50:32 (UTC)" aria-label="Posted on 09 Jul">09 Jul</time>, <time pubdate class="dt-updated" datetime="2015-06-18T13:59:49+0000" title="Time posted: 18 Jun 2015, 13:59:49 (UTC)" aria-label="Posted on 18 Jun">18 Jun</time>, <time pubdate class="dt-updated" datetime="2015-06-18T12:56:47+0000" title="Time posted: 18 Jun 2015, 12:56:47 (UTC)" aria-label="Posted on 18 Jun">18 Jun</time>, <time pubdate class="dt-updated" datetime="2015-06-18T12:41:42+0000" title="Time posted: 18 Jun 2015, 12:41:42 (UTC)" aria-label="Posted on 18 Jun">18 Jun</time>]';
var res = str.match(/datetime="(.*?)"/gi); 
alert (res[0]);