Regular Expressions (regex) are used regularly in projects to match strings within our content. For example, take the following HTML:

<div id="Mary">
  Mary had a lamb

Apply the following Tritium:

$("/div[@id='Mary']") {
  text() {
    replace(/lamb/, "turtle")

The resulting HTML would be:

<div id="Mary">
  Mary had a turtle

Regex is used in the replace() function to specify a string of letters to be matched. This is a pretty simple example — regex can get quite complex.

The most common location for regex is in the mappings.ts file, where specific Tritium scripts are imported when matching a particular URL (learn more about the file here). The matching of a URL is done with regex, which can be anything from something as simple as:


which would map a URL with “search” in it, to something as complex as:


which matches a URL with either “index” in it or a line-ender (which is useful for homepages, which may not have anything afterwards).

Regex is a complex and sizable pattern matching standard. Look through the W3Schools guide linked below for more detailed guidance.

Rubular is also a very useful way to test and troubleshoot regex.

Common Regular Expressions

Here are some common regular expressions that Moovweb uses:

[a-z]         - matches any character from "a" to "z" (lowercase only)
^[a-z]        - matches any character NOT from "a" to "z"
\w            - matches any word character
\W            - matches any NON-word character
\d            - matches any digit character
\s            - matches any whitespace character
x{n}          - matches a string of N X characters
                (e.g., \w{3} matches a string of three word characters)


Online Testers