feat(facade): add RegExpWrapper.replaceAll to replace all matches using the provided function
This commit is contained in:
@ -215,6 +215,20 @@ class RegExpWrapper {
|
||||
static Iterator<Match> matcher(RegExp regExp, String input) {
|
||||
return regExp.allMatches(input).iterator;
|
||||
}
|
||||
|
||||
static String replaceAll(RegExp regExp, String input, Function replace) {
|
||||
final m = RegExpWrapper.matcher(regExp, input);
|
||||
var res = "";
|
||||
var prev = 0;
|
||||
while(m.moveNext()) {
|
||||
var c = m.current;
|
||||
res += input.substring(prev, c.start);
|
||||
res += replace(c);
|
||||
prev = c.start + c[0].length;
|
||||
}
|
||||
res += input.substring(prev);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
class RegExpMatcherWrapper {
|
||||
|
@ -347,6 +347,21 @@ export class RegExpWrapper {
|
||||
regExp.lastIndex = 0;
|
||||
return {re: regExp, input: input};
|
||||
}
|
||||
static replaceAll(regExp: RegExp, input: string, replace: Function): string {
|
||||
let c = regExp.exec(input);
|
||||
let res = '';
|
||||
regExp.lastIndex = 0;
|
||||
let prev = 0;
|
||||
while (c) {
|
||||
res += input.substring(prev, c.index);
|
||||
res += replace(c);
|
||||
prev = c.index + c[0].length;
|
||||
regExp.lastIndex = prev;
|
||||
c = regExp.exec(input);
|
||||
}
|
||||
res += input.substring(prev);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
export class RegExpMatcherWrapper {
|
||||
|
Reference in New Issue
Block a user