replaceAllMapped abstract method
String replaceAllMapped(Replace all substrings that match from
by a string computed from the match.
Returns a new string in which the non-overlapping substrings that match from
(the ones iterated by from.allMatches(thisString)
) are replaced by the result of calling replace
on the corresponding Match object.
This can be used to replace matches with new content that depends on the match, unlike replaceAll where the replacement string is always the same.
The replace
function is called with the Match generated by the pattern, and its result is used as replacement.
The function defined below converts each word in a string to simplified 'pig latin' using replaceAllMapped
:
pigLatin(String words) => words.replaceAllMapped( new RegExp(r'\b(\w*?)([aeiou]\w*)', caseSensitive: false), (Match m) => "${m[2]}${m[1]}${m[1].isEmpty ? 'way' : 'ay'}"); pigLatin('I have a secret now!'); // 'Iway avehay away ecretsay ownay!'
Source
String replaceAllMapped(Pattern from, String replace(Match match));
© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dartlang.org/stable/1.24.3/dart-core/String/replaceAllMapped.html