![](/img/trans.png)
[英]How can I replace a keyword but in HTML tags by JavaScript regular expression?
[英]Regular expression - replace multiple html tags with one
是否有正則表達式或任何方式替換以下內容:
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
如果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'>", "");
有意見認為正則表達式不應用於處理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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.