replace static method

String replace(
  1. String word,
  2. List rule
)

Implementation

static String replace(String word, List<dynamic> rule) {
  final regex = rule[0] as RegExp;
  //print('regex: $regex');

  // Use the first element of the rule as a RegExp to match in the word.
  // The second element of the rule is used as a string to replace the match.
  return word.replaceFirstMapped(regex, (match) {
    // Interpolate the replacement string using arguments from the match.

    final groups = match.groups([match.groupCount]);

    final args = groups.map((e) => e!).toList();
    final group = match.group(0);
    final String result = interpolate(rule[1] as String, args);

    // If the match is an empty string, use the previous character of the word.
    if (group!.isEmpty) {
      return restoreCase(word[match.start - 1], result);
    }
    // Otherwise, use the match as the source to restore the case.
    return restoreCase(match.group(0)!, result);
  });
}