在 StyleBook 中允许特殊字符以其字面形式存在

Last published : Oct 06, 2026
在 StyleBook 定义中,当字符串中的特殊字符必须以其字面形式存在时,您可以使用逐字字符串。这些字符串可以包含转义字符、反斜杠、引号、括号、空格、方括号等。
在 StyleBook 中,您可以使用逐字字符串以其字面形式包含 NetScaler 策略表达式。策略表达式通常包含特殊字符。如果没有逐字字符串,您必须通过将字符串分解为子字符串来转义特殊字符。
要创建逐字字符串,请按如下方式将字符串封装在特殊字符之间:
~{string}~
您可以在 StyleBook 表达式中使用逐字字符串。
注意
请勿在输入字符串中使用字符序列 }~,因为此序列表示逐字字符串的结束。
示例:
---
name: test-deny-url
namespace: citrix.adc.stylebooks
version: "6.2"
display-name: StyleBook for creating an AppFw profile
description: This Stylebook configures an AppFw config.
schema-version: "1.0"
import-stylebooks:
  -
    namespace: netscaler.nitro.config
    prefix: ns
    version: "13.0"
parameters:
  -
    name: name
    type: string
    required: true
substitutions:
 default-deny-urls-regex:
     "Command_injection_attack": str("([ /=]|\\t|\\n)(ls|cat)([ ;'" + "\\\"&].*)?$")
     "Command_injection_attack2": ~{([ /=]|\t|\n)(ls|rm|cat)([ ;'\"&].*)?$}~
components:
  -
    name: appfw-profile-comp
    type: ns::appfwprofile
    properties:
      name: $parameters.name
      type: ["XML"]
    components:
          -
            name: appfw-profile-denyurl-default-binding-subcomp-all
            type: ns::appfwprofile_denyurl_binding
            properties:
                name: $parent.properties.name
                denyurl?: $substitutions.default-deny-urls-regex["Command_injection_attack"]
                state?: "ENABLED"
          -
            name: appfw-profile-denyurl-default-binding-subcomp-all2
            type: ns::appfwprofile_denyurl_binding
            properties:
                name: $parent.properties.name
                denyurl?: $substitutions.default-deny-urls-regex["Command_injection_attack2"]
                state?: "ENABLED"
在此示例中,default-deny-urls-regex 替换具有逐字表达式。
  • "Command_injection_attack": str("([ /=]|\\t|\\n)(ls|cat)([ ;'" + "\\\"&].*)?$"):StyleBook 将其作为正则表达式处理。
  • "Command_injection_attack2": ~{([ /=]|\t|\n)(ls|rm|cat)([ ;'\"&].*)?$}~:StyleBook 将其作为逐字表达式处理。此字符串中的特殊字符采用字面形式。

连接多个逐字字符串

您可以将逐字字符串与常规字符串或带有插值的字符串连接起来。这样做时,StyleBook 仅跳过对逐字字符串的解释。使用加号 (+) 运算符在字符串之间进行连接。
示例:
value: "~{\"id\": \"}~ + %{$atom.key}% + ~{\", \"value\": \"}~ + %{$atom.value}% + ~{\"}~"
在此示例中,%{$atom.key}% 和 %{$atom.value}% 会被解释。其余部分则跳过解释。