whitelist function

String whitelist (String str, String chars)

remove characters that do not appear in the whitelist.

The characters are used in a RegExp and so you will need to escape some chars.

Implementation

String whitelist(String str, String chars) {
  return str.replaceAll(new RegExp('[^' + chars + ']+'), '');
}