replace method

String replace(
  1. String source,
  2. String replacement
)

replace source with replacement attending global flag. replacemnt could contain $n, notation. Don't support: $& - the matched substring. $` - the portion of the string that precedes the matched substring. $' - the portion of the string that follows the matched substring. Example: RegExpExtended r = new RegExpExtended(r'(string).$'); String re = r.replace('This is a string.', r'new $1.'); re == 'This is a new string.'

Implementation

String replace(String source, String replacement) {
  final dollar = RegExp(r'\$\d+');
  String dollarN;
  Match match;
  var _replacement = replacement;

  if (dollar.hasMatch(_replacement)) {
    match = _thisRE.firstMatch(source);
    if (match != null) {
      for (var i = 0; i <= match.groupCount; i++) {
        // ignore: prefer_interpolation_to_compose_strings
        dollarN = '\$' + i.toString();
        _replacement = _replacement.replaceAll(dollarN, match[i]);
      }
    }
  }

  return global
      ? source.replaceAll(_thisRE, _replacement)
      : source.replaceFirst(_thisRE, _replacement);
}