formatCardnumber function

String formatCardnumber(
  1. String input
)

Implementation

String formatCardnumber(String input) {
  input = input.replaceAll(RegExp(r'\s'), ''); // Remove existing spaces
  RegExp fourDigits = RegExp(r".{1,4}");
  Iterable<RegExpMatch> matches = fourDigits.allMatches(input);
  List<String> groups = matches.map((match) => match.group(0)!).toList();
  String formattedString = groups.join(' ');
  return formattedString;
}