Development build 586e1bd4
Typst文档简体中文版v0.15.1 + dev 586e1bd4

regex自始可用Go to source

A regular expression.

Can be used as a show rule selector or with string methods.

Visit this website for a complete specification of the supported syntax.

With string methods

Regular expressions can be used with string methods like find, split, replace, match, or matches. See the documentation of these methods for more details.

#"a,b;c".split(regex("[,;]"))

With show rules

Regular expressions can also be used with show rules to match on and transform text in the document. For example, below, we are turning all numbers red.

#show regex("\\d+"): set text(red) The numbers 1 to 10.

Sometimes, you may also want to combine both uses, by first matching on text with a show rule and then rematching on the text to extract a specific capture group. In this case, it can be convenient to store the regular expression in a variable instead of repeating it twice.

#let pattern = regex("\|([^|]*)\|") #show pattern: it => { let m = it.text.match(pattern) let inner = m.captures.first() highlight(inner) } A |handy-dandy| highlighter!

构造函数
Question mark
自始可用Go to source

Create a regular expression from a string.

regex(str) → regex

regex
str
必需参数
Question mark
位置参数
Question mark

The regular expression as a string.

Both Typst strings and regular expressions use backslashes for escaping. To produce a regex escape sequence that is also valid in Typst, you need to escape the backslash itself (e.g., writing regex("\\\\") for the regex \\). Regex escape sequences that are not valid Typst escape sequences (e.g., \d and \b) can be entered into strings directly, but it’s good practice to still escape them to avoid ambiguity (i.e., regex("\\b\\d")). See the list of valid string escape sequences.

If you need many escape sequences, you can also create a raw element and extract its text to use it for your regular expressions: regex(`\d+\.\d+\.\d+`.text).