次のようなテキストの段落があります。
<p>
<span style='font-family:arial'>
Some text
</span>
</p>
また
<p>
<strong>
Some more text
<strong>
</p>
また
<p>
<strong>
<em>
Yet more text
</em>
</strong>
</p>
ネストされたタグがいくつあっても、 を使用するだけでテキストだけを取得できます$('p').text()
。問題は<br>
、途中でポップアップするときです。その場合、テキストが入っているタグが何であれ、分割されます。たとえば、次のようになります。
<p>
<strong>
Some more text
</strong>
</p>
これに変わります:
<p>
<strong>
Some
</strong>
<br />
<strong>
more text
</strong>
</p>
ご覧のとおり、<strong>
タグには 1 つだけでなく 2 つのテキスト ノードがあります。私がやりたいのは、元の親タグを持つテキストだけを取得し、<br>
別のテキストノードとして扱いますが、<br>
誘導タグ分割の侵入はありません。たとえば、上記の 2 ノードの HTML を考えると、これを返す関数が必要なだけです。
<p>
<strong>
Some
<br />
more text
</strong>
</p>
いくつかの特定の形式ではそれで問題ありませんが、保持する必要があるさまざまな種類の HTML ネスト (<p><strong><em>
または<p><em><strong>
またはなど) が存在する可能性があります<p><strong><span>
。
編集
ループで迷子になるよりも、最も簡単な方法$('p').html()
は<br>
? 左側には<br>
終了タグがあり、右側には開始タグがあります。これに対する正規表現ソリューションはありますか?