KScript docs
EN

Types and validation

Regular expressions

A regex literal is written between slashes, with its flags after them. Named groups come back by name:

<let name="date" value="/(?&lt;year>\d{4})-(?&lt;month>\d\d)/" />
<let name="found" value="$date->match('Due 2026-11')" />
<echo>{ $found.groups.year } { $date->test('no date') }</echo>
<echo>{ $date->replace('2026-11', '${month}/${year}') }</echo>
<echo>{ /\s*,\s*/->split('tea, coffee ,milk') }</echo>
<echo>{ /\d+/->match_all('3 cats, 12 dogs')->map(.text) }</echo>
$date = /(?<year>\d{4})-(?<month>\d\d)/
$found = $date->match('Due 2026-11')
echo "{ $found.groups.year } { $date->test('no date') }"
echo "{ $date->replace('2026-11', '${month}/${year}') }"
echo "{ /\s*,\s*/->split('tea, coffee ,milk') }"
echo "{ /\d+/->match_all('3 cats, 12 dogs')->map(.text) }"

->match() returns { text, index, groups, captures }, or null when nothing matches. A regex that takes longer than a second to match stops with an error, so a bad pattern can't hang a server. In XML, write < inside an attribute or text as &lt;.

Edit this page on GitHub