【问题标题】:How do I mutate a tritium variable string?如何变异氚变量字符串?
【发布时间】:2013-07-23 22:20:01
【问题描述】:

我已经将整个类列表从正文中提取到一个变量中,如下所示:

$page_code = fetch("self::body/@class")

但我只想抢一堂课。我尝试在变量内容上使用 replace() 和正则表达式,但我认为我的语法有问题:

$page_code {
  text() {
    replace(/[^A-Z]*/, '')
  }
  log("@@@@@@@@@@@@@@ page code is " + $page_code)
}

【问题讨论】:

    标签: regex variables tritium


    【解决方案1】:

    我认为你想要的代码是:

    $page_code {
      replace(/[^A-Z]*/, '')
    }
    log("@@@@@@@@@@@@@@ page code is " + $page_code)
    

    由于 $page_code 已经是一个字符串,您不需要打开 text() 范围。另外,log() 语句应该在外面,否则它只会记录 $page_scope 的当前值。

    在这里查看:http://tester.tritium.io/8ccb7ef99a0fd2fcbd60bd74cc137b040f57555e

    【讨论】:

    • 哦,日志输出让我很困惑,哈哈。我以为什么都没有发生,但现在可以了,谢谢!
    【解决方案2】:

    $page_code 已经是 text() 所以打开一个文本范围什么都不做。

    在此处查看此示例:

    http://tester.tritium.io/3a9f3bfcffe41ab39a9f6927965d5b173692485f

    代码:

    html() {
      $("/html") {
        $page_code = fetch("./body/@class")
        log("@@@@@@@@@@@@@@ page code is " + $page_code)
        $page_code {
          replace(/[^A-Z]/, '')
        }
        log("@@@@@@@@@@@@@@ page code is " + $page_code)
    
        # better way to do this
        $("./body") {
          attribute("class") {
            value() {
              replace(/[^A-Z]/, '')
            }
          }
        }
      }
    }
    

    第二种方式是编辑body class并为你设置新的body class。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-24
      • 1970-01-01
      • 1970-01-01
      • 2016-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多