如何替换 Dart 中字符串中除 space 字符以外的所有字符

How to replace all characters in a string in Dart except the space character

String text = "replace all characters" --> 22 characters

text.replaceAll(RegExp('.'), '*');   

// result -> ********************** --> 22 characters

这样一来,所有的角色都变了。如何排除 space 字符。

// I want it this

// result -> ******* *** ********** -> Characters 8 and 13 are empty.

你想要的是负匹配:

void main() {
  String text = "replace all characters";
  print(text.replaceAll(RegExp('[^ ]'), '*'));
  // ******* *** **********
}

[^ ]将匹配任何不是 space.

的字符