Core\Regex::replace
Core\Regex::replace(
string $subject,
Core\Regex\Pattern|string $pattern,
string $replacement,
{limit?: uint},
): stringFinds every match of a pattern in a text and replaces it with another text. It returns the new text. If there is no match, it returns the same text.
The replacement text can contain parts of the match. $0 is the whole match. $1, $2 and so on are the groups of the pattern, counted from the left. A named group is written ${name}. Write ${1}0 when a digit comes right after a group, and $$ for one dollar sign. A group that the pattern does not have inserts nothing. The limit option replaces only the first matches. \1 is not a group in the replacement text. Text from a visitor is not allowed as the replacement, and does not compile. To insert it, return it from the function of Core\Regex::replaceWith.
Good to know: the replacement is the same text for every match. To build a different text for each match with your own code, use Core\Regex::replaceWith.
The examples show a simple replacement and limit, the use of groups, and hiding card numbers in a log line.
Parameters
$subjectstringThe text to search.$patternCore\Regex\Pattern|stringACore\Regex\PatternfromCore\Regex::compile, or a pattern string with no options. Ataintedstring does not compile here.$replacementstringThe text that replaces each match.$1to$99insert a numbered group, and${name}inserts a named group. Write${1}0when a digit follows the group.$$is one$. A group the pattern does not have inserts nothing.\1is not a group. Ataintedstring does not compile here. To insert text from outside, return it from the function ofreplaceWith.
Options
limituintoptionalHow many matches to replace, counted from the start of the text. The default is every match.0replaces nothing.
Return value
Returns string. The text with the matches replaced. It is the same text if there is no match.
Errors
RuntimeError$patternis not a valid regular expression, or matching it against$subjectneeds more steps than the limit allows.
Examples
Tidy the spaces in a text
<?nvs// Shows how to replace every match of a pattern, or only the first ones.
string $title = 'Blue summer dress';
// `\s+` matches one or more spaces. Each run of spaces becomes one space.// This prints "Blue summer dress".echo Core\Regex::replace($title, '\s+', ' '), "\n";
// `limit: 1` replaces only the first match. This prints "Blue-summer dress".echo Core\Regex::replace($title, '\s+', '-', { limit: 1 }), "\n";
// There is no match here, so the text does not change. This prints "Blue summer dress".echo Core\Regex::replace($title, '[0-9]', '#'), "\n";Blue summer dressBlue-summer dressBlue summer dressChange the order of a date
<?nvs// Shows how to use the groups of a pattern in the replacement text.
string $date = '2026-09-27';
// `$1`, `$2` and `$3` are the texts of the three groups, counted from the left.// This prints "27/09/2026".echo Core\Regex::replace($date, '(\d{4})-(\d{2})-(\d{2})', '$3/$2/$1'), "\n";
// A named group is written `${name}` in the replacement. This prints "27.09.2026".echo Core\Regex::replace($date, '(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})', '${day}.${month}.${year}'), "\n";
// `$0` is the whole match. `$$` is one dollar sign. This prints "Total: $19.99 (19.99)".echo Core\Regex::replace('Total: 19.99', '\d+\.\d{2}', '$$$0 ($0)'), "\n";27/09/202627.09.2026Total: $19.99 (19.99)Hide card numbers in a log line
<?nvs// Shows how to hide card numbers before a line is written to a log file.
// The pattern is compiled once. It finds 12 digits followed by 4 more digits,// and keeps the last 4 digits in group 1.Core\Regex\Pattern $card = Core\Regex::compile('\b\d{12}(\d{4})\b');
array<string> $lines = [ 'order 1042 paid with card 4111111111111111', 'order 1043 paid with card 5500000000000004, refund to 4000056655665556', 'order 1044 paid by bank transfer',];
// Every card number becomes "************" and its last 4 digits.// A line with no card number does not change.foreach ($lines as string $line) { echo Core\Regex::replace($line, $card, '************$1'), "\n";}order 1042 paid with card ************1111order 1043 paid with card ************0004, refund to ************5556order 1044 paid by bank transfer