alphanumericOnly method
Removes all non-alphanumeric characters from this string
keepSpaces
Whether to keep spaces (defaults to false)
Example:
print('Hello, World!'.alphanumericOnly()); // "HelloWorld"
print('Hello, World!'.alphanumericOnly(keepSpaces: true)); // "Hello World"
Implementation
String alphanumericOnly({bool keepSpaces = false}) {
final pattern = keepSpaces ? r'[^a-zA-Z0-9\s]' : r'[^a-zA-Z0-9]';
return replaceAll(RegExp(pattern), '');
}