replaceStringMarks function

String? replaceStringMarks(
  1. String? s,
  2. RegExp marksPattern,
  3. String markResolver(
    1. String markName
    )
)

Replaces String s marks using marksPattern and markResolver.

marksPattern A RegExp with the mark pattern and a 1 group with the mark name. markResolver Resolves mark name (from marksPattern group(1)) to a value to replace the marksPattern match.

Implementation

String? replaceStringMarks(String? s, RegExp marksPattern,
    String Function(String markName) markResolver) {
  if (s == null) return null;

  var matches = marksPattern.allMatches(s);

  var strFilled = '';

  var pos = 0;
  for (var match in matches) {
    var prev = s.substring(pos, match.start);
    strFilled += prev;

    var markName = match.group(1)!;

    var val = markResolver(markName);

    strFilled += val;

    pos = match.end;
  }

  if (pos < s.length) {
    var prev = s.substring(pos);
    strFilled += prev;
  }

  return strFilled;
}