javascript正则表达式为html

问题描述 投票:0回答:1

我有一些HTML内容......

<p>{{Specifications}}</p>
<p>some content down here</p>
<p>{{/Specifications}}</p>

<p>{{Photos}}</p>
<h2>some content down here</h2>
<p>some content down here</p>
<p>some content down here</p>
...
<p>{{/Photos}}</p>

通过Jquery我希望获得每个集合的数组返回{{*}}值和包含的html在两个单独的变量中,以便我可以将它放入一些div等,并动态地将其转换为产品页面的选项卡。

一旦我得到正则表达式将它扔进一个数组我可以将它放入html等,它只是我正在努力的正则表达式:P

javascript jquery regex
1个回答
0
投票

答案是不使用正则表达式。

因为你已经在使用jquery,它使用sizzle来使用css选择器从dom(或某些html)中选择元素。

由于您可以控制标记,因此可以转动

<p>{{Photos}}</p>
<h2>some content down here</h2>
<p>some content down here</p>
<p>some content down here</p>
...
<p>{{/Photos}}</p>

成:

<div class="section" data-section-name="photos">
    <p class="section-tag start">{{Photos}}</p>
    <h2>some content down here</h2>
    <p>some content down here</p>
    <p>some content down here</p>
    ...
    <p class="section-tag end">{{/Photos}}</p>
</div>

然后,您可以使用选择部分

$('div.section[data-section-name="' + someVar + '"]);

然后使用jQuery删除'.section-tag'并且你有你需要的标记,你也可以删除<p>{{*}}</p>标签,如果它们仅用于划分部分,你可以使用html打开和关闭标签,而不是解析其他标记你'用于打开和关闭部分。

© www.soinside.com 2019 - 2024. All rights reserved.