regexflutterdart

How to use dart to remove all but digits from string using RegExp


Can someone show me how to use dart language to remove all but digits from a string?

Tried this but it does not seems to work

input.replaceAll("\\D", "");

Solution

  • You need to use

    input.replaceAll(RegExp(r'\D'), '');
    

    See the replaceAll method signature: String replaceAll (Pattern from, String replace), from must be a Pattern class instance.

    Note that r'\D', a raw string literal, is a more convenient way to define regular expressions, since regular string literals, like '\\D', require double escaping of backslashes that form regex escapes.