onlyNumbers method Null safety

String? onlyNumbers()

Returns only the numbers from the String.

Example

String foo = '4*%^55/es4e5523nt1is';
String onyNumbers = foo.onlyNumbers() // returns '455455231'

Implementation

String? onlyNumbers() {
  if (this == null) {
    return null;
  }
  if (this!.isEmpty) {
    return this;
  }
  // ignore: unnecessary_raw_strings
  var regex = RegExp(r'([^0-9]+)');
  return this!.replaceAll(regex, '');
}