onlyLatin method Null safety

String? onlyLatin()

Returns only the Latin characters from the String.

Example

String foo = '4*%^55/es4e5523nt1is';
String onlyLatin = foo.onlyLatin(); // returns 'esentis'

Implementation

String? onlyLatin() {
  if (this == null) {
    return null;
  }
  if (this!.isEmpty) {
    return this;
  }
  // ignore: unnecessary_raw_strings
  var regex = RegExp(r'([^a-zA-Z\s]+)');
  return this!.replaceAll(regex, '');
}