replaceAllWithCallback function

String replaceAllWithCallback(
  1. String input,
  2. RegExp pattern,
  3. String replace(
    1. Match match
    )
)

Replaces every match of pattern in input with the result of replace.

replace is called once per match and receives the Match, letting the replacement depend on captured groups. Returns input unchanged when there are no matches.

Example:

replaceAllWithCallback('a1b2', RegExp(r'\d'), (m) => '[${m[0]}]'); // 'a[1]b[2]'

Audited: 2026-06-12 11:26 EDT

Implementation

String replaceAllWithCallback(
  String input,
  RegExp pattern,
  String Function(Match match) replace,
) => input.replaceAllMapped(pattern, replace);