【发布时间】:2016-03-27 21:20:24
【问题描述】:
当出现特定变量时,我需要将我的html 分成两部分。
这是一个例子:
<h1>I can have any kind of text here</h1>
<p> </p>
<p><em><strong>When I see this variable :)</strong> I want to rebuild my html.</em></p>
最后我想要什么:
<h1>I can have any kind of text here</h1>
<p> </p>
<p><em><strong>When I see this</strong></em></p>
<p><em><strong> :)</strong> I want to rebuild my html.</em></p>
这是我的想法(告诉我是否有更好的方法):
- 拆分我的变量
- 通过正则表达式获取所有标签? (我只知道正则表达式告诉我是真是假)
- 将所有未封闭的标签放在一个堆栈上?
- 循环两次(数组拆分 1 和数组拆分 2)
编辑示例 2:
<ol>
<li>
<h2>I can have any kind of text here 1</h2>
</li>
<li>
<h2><strong>I can have any kind of text here 2 variable</strong></h2>
</li>
<li>
<h2><strong>I can have any kind of text here 3</strong></h2>
</li>
<li>
<h2>I can have any kind of text here 4</h2>
</li>
<li><em><strong>When I see this</strong></em></li>
</ol>
<p> </p>
...
<ol>
<li>
<h2>I can have any kind of text here 1</h2>
</li>
<li>
<h2><strong>I can have any kind of text here 2 variable </strong></h2>
</li>
</ol>
变量
<ol>
<li>
<h2><strong>I can have any kind of text here 3</strong></h2>
</li>
<li>
<h2>I can have any kind of text here 4</h2>
</li>
<li><em><strong>When I see this</strong></em></li>
</ol>
<p> </p>
示例 3:
<p><a href="test">I can have any kind of text of varaible here even clickable.</a></p>
...
<p><a href="test">I can have any kind of text of</a></p>
变量
<p><a href="test">here even clickable.</a></p>
【问题讨论】:
-
第 5 步:忘记第 1-4 步,改用解析器?
-
确实有很多 html 解析器。