【发布时间】:2017-01-26 15:40:00
【问题描述】:
这是我的示例字符串...
<span> </span><span class="citation_text" id="_148511159">Rawls, Wilson.
<i>Where the Red Fern Grows: The Story of Two Dogs and a Boy</i>. Garden City, NY: Doubleday, 1961. Print.</span>
我想删除 < 和 > 中的所有文本,但保留 <i> 和 </i> 中的文本。
我得到的最接近的是这段代码
string.replace(/<.[^i]+?>/g,"")
但是它返回这个
<span class="citation_text" id="_148511159">Rawls, Wilson.
<i>Where the Red Fern Grows: The Story of Two Dogs and a Boy</i>. Garden City, NY: Doubleday, 1961. Print.
如何让它删除保存在 < 和 > 中的最终跨度?
谢谢!
更新:这就是我想要的输出。
Rawls, Wilson. <i>Where the Red Fern Grows:
The Story of Two Dogs and a Boy</i>. Garden City, NY: Doubleday, 1961. Print.
【问题讨论】:
-
您能否显示您发布的示例字符串应该产生的确切预期输出?
-
您可以编写几行 jQuery 到您的 span 的
unwrap()和contents(),而不是针对复杂的正则表达式。只是说。 -
你为什么要这样做?
-
@DavidThomas :谢谢,我刚刚将其添加到帖子中! Oriol:我首先将 innerHTML 内容作为字符串获取。
标签: javascript regex string replace