How do I mutate a tritium variable string?

300 Views Asked by At

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)
}
2

There are 2 best solutions below

1
On BEST ANSWER

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

0
On

$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.