Janaka
Janaka

Reputation: 2795

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", "");

Upvotes: 14

Views: 8491

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626893

You need to use

input.replaceAll(new 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.

Upvotes: 28

Related Questions