是否有正则表达式或任何替换以下内容的方法:
some content...<span style='color:red'>t</span><span style='color:red'>e</span><span style='color:red'>s</span><span style='color:red'>t</span> some content
用这个?
some content... <span style='color:red'>test</span> some content
答案 0 :(得分:6)
如果HTML标记完全相同,您只需使用String.replace。
var oldStr = "<span style='color:red'>t</span><span style='color:red'>e</span><span style='color:red'>s</span><span style='color:red'>t</span>";
var newStr = oldStr.replace("</span><span style='color:red'>", "");
答案 1 :(得分:0)
有观点认为不应使用正则表达式来处理HTML。
如果您同意,可以使用以下XSLT转换。
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="html" doctype-public="XSLT-compat"
encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="span">
<xsl:copy>
<!-- Copy attributes and text from the current span -->
<xsl:apply-templates select="@*|node()"/>
<xsl:variable name="nextSpans" select="following-sibling::span"/>
<!-- Copy text from following spans -->
<xsl:for-each select="following-sibling::node()">
<xsl:variable name="ind" select="position()"/>
<xsl:if test="generate-id()=generate-id($nextSpans[$ind])">
<xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:copy>
</xsl:template>
<!-- Switch off processing of "following" spans -->
<xsl:template match="span[preceding-sibling::node()[1]/name()='span']"/>
<xsl:template match="@*|node()">
<xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:template>
</xsl:stylesheet>