Flutter:如何使用regex分割包含html标记的字符串列表



我试图将带有html标记的句子列表拆分如下:
<a href="sy.com";id=";sy">天空<a>是清楚的,并且<a href="st.com";class=";st">星形<a>正在闪烁
他们是<a href="etc.com";id=";等等>兴奋<a>去看他们的第一只树懒<道格拉斯想出了最好的办法&lt;a>

我想要的结果如下:
[天空,<a href="sy.com"id="sy>天空</a>,是清澈的,而星星,lt;a hreface="st.com>class=",去看他们的第一只树懒。]
[道格拉斯认为,要想lt;a href="etc.com"id="et>成功lt;/a>,最好的方法就是做他一生都在做的事情的相反的事情。]

我怎么能用RegExp做到这一点,才能得到以上结果?

例如,这是您的字符串:

String str =
"The <a href="sy.com" id="sy">sky</a> is clear and the <a href="st.com" class="st">stars</a> are twinkling. They were <a href="etc.com" id="et">excited</a> to see their first sloth.  Douglas figured the best way to <a href="etc.com" id="et">succeed</a> was to do the <a href="opt.com" class="op">opposite</a> of what he'd been doing all his life.";

你们家族是这样分裂的:

final reg = RegExp("<[^>]*>");
var result = str.split(reg);
print('result = $result'); // [The , sky,  is clear and the , stars,  are twinkling. They were , excited,  to see their first sloth.  Douglas figured the best way to , succeed,  was to do the , opposite,  of what he'd been doing all his life.]

如果你想用标签,试试这个(坦克到@JovenDev(:

final reg = RegExp("(?=<a)|(?<=/a>)");
var result = str.split(reg);
print('result = $result'); // [The , <a href="sy.com" id="sy">sky</a>,  is clear and the , <a href="st.com" class="st">stars</a>,  are twinkling. They were , <a href="etc.com" id="et">excited</a>,  to see their first sloth.  Douglas figured the best way to , <a href="etc.com" id="et">succeed</a>,  was to do the , <a href="opt.com" class="op">opposite</a>,  of what he'd been doing all his life.]

最新更新