replaceBetween function
Replaces text within a range starting and ending with a specific character in a given string with another string. For example, given the string abc<DEF>ghi, to change <DEF> to def, use replaceBetween('abc<DEF>ghi', '<', '>', 'def'). The result would be abcdefghi.
Deletes strings in the range if replaceWith is not specified.
Implementation
String replaceBetween(String str, String startChar, String endChar, String replaceWith) {
final RegExp specialCharacters = RegExp(r'[.*+?^${}()|[\]\\]');
final String startCharRegExp = specialCharacters.hasMatch(startChar) ? '\\$startChar' : startChar;
final String endCharRegExp = specialCharacters.hasMatch(endChar) ? '\\$endChar' : endChar;
return str.replaceAll(RegExp('$startCharRegExp.*?$endCharRegExp'), replaceWith);
}