我有这样的 HTML:
<div id='main-post' class='my-post'>
$start
content 1
$end
$start
content 2
$end
$start
content 3
$end
$start
content 4
$end
</div>
现在,我要解析 $main-post
的文本内容每个之间$start
至$end
分块到数组中并得到以下结果:
thecontent[1] == "content 1"
thecontent[2] == "content 2"
...
最佳答案
这是一个在$start
和$end
之间匹配 block 的正则表达式问题:
var content = $('#main-post').text(),
re = /\$start([\s\S]*?)\$end/g,
thecontent = [];
while ((match = re.exec(content)) !== null) {
thecontent.push($.trim(match[1]))
}
由于 JavaScript 没有“点匹配所有”修饰符,因此我使用 [\s\S]
技巧来完成同样的事情。
旧答案
这里摸黑,但是如果你想获取#main-post
下每个子节点的文本内容:
var thecontent = $('#main-post')
.children()
.map(function() {
return this.textContent || this.innerText || '';
})
.get();
关于javascript - 将同一个框中的内容捕获到数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20793641/