escape method

String escape()

Escape a string.

Implementation

String escape() {
  if (this == null) {
    throw ArgumentError('string: $this');
  }
  return this!.replaceAllMapped(RegExp(r'([\\"])'), (match) {
    final char = match.group(1)!;
    if (char == r'\') {
      return r'\\';
    } else if (char == '"') {
      return r'\"';
    } else {
      return char;
    }
  });
}