feat(facade): add RegExpWrapper.replaceAll to replace all matches using the provided function

This commit is contained in:
vsavkin
2016-03-23 13:41:18 -07:00
committed by Victor Savkin
parent aa966f5de2
commit 91999e016e
3 changed files with 35 additions and 0 deletions

View File

@ -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 {

View File

@ -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 {