简体   繁体   中英

How do I mutate a tritium variable string?

I've fetched the entire list of classes from the body into a variable like so:

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

But I only want to grab one class out. I tried using replace() and a regex on the variable contents but I think something's up with my syntax:

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

I think the code you want is:

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

Since $page_code is already a string, you don't need to open up a text() scope. Also, the log() statement should be outside, or else it will just log the current value of $page_scope.

See it here: http://tester.tritium.io/8ccb7ef99a0fd2fcbd60bd74cc137b040f57555e

$page_code is already text() so opening a text scope does nothing.

View this example here:

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

Code:

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]/, '')
        }
      }
    }
  }
}

The second way edits the body class and sets the new body class for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM