正則表達式應該驗證這樣的話A23-abcefghijk正則表達式錯誤
<xsl:variable name="myRegex">
<xsl:value-of select="a\\d{2}\\-[a-zA-Z]+" />
</xsl:variable>
但我正在逐漸語法錯誤,我試圖轉義字符,但沒有找到任何解決方案尚未
正則表達式應該驗證這樣的話A23-abcefghijk正則表達式錯誤
<xsl:variable name="myRegex">
<xsl:value-of select="a\\d{2}\\-[a-zA-Z]+" />
</xsl:variable>
但我正在逐漸語法錯誤,我試圖轉義字符,但沒有找到任何解決方案尚未
您的變量的值必須是一個字符串,所以需要引用它的select
裏面...
<xsl:variable name="myRegex" select="'a\d{2}-[a-zA-Z]+'"/>
像現在的凝固酶原ssor正試圖將您的select
評估爲XPath表達式。
另請注意,如果您使用XSLT 1.0,則必須使用extension function(以及支持它的處理器)。
這裏有一個2.0例子...
XML輸入
<doc>
<test>a23-abcefghijk</test>
<test>qwerty</test>
</doc>
XSLT 2.0
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="myRegex" select="'a\d{2}-[a-zA-Z]+'"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="test">
<test matches="{if (matches(.,$myRegex)) then 'yes' else 'no'}">
<xsl:value-of select="."/>
</test>
</xsl:template>
</xsl:stylesheet>
XML輸出
<doc>
<test matches="yes">a23-abcefghijk</test>
<test matches="no">qwerty</test>
</doc>
<xsl:variable name="myRegex">
<xsl:value-of select="regExp:match('This is a test string', '(a\d{2}\-[a-zA-Z]+)', 'g'" />
</xsl:variable>
你嘗試單引號和斜槓嗎? ( ) –
我認爲您使用雙反斜槓是因爲使用其他主機語言。在C或Java中,字符串文字中的反斜線需要轉義爲「\\」。 XML或XSLT中的情況並非如此,反斜槓沒有特殊含義。 –