Skip to content
This repository was archived by the owner on May 21, 2024. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions lib/invokables/invokableregexp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,38 @@ class InvokableRegExp extends Object with Invokable {
Map<String, Function> methods() {
return {
'test': (String input) => regExp.hasMatch(input),
//'exec': (String input) => regExp.allMatches(input).toList()

'exec': (String input) {
var match = regExp.firstMatch(input);
return match != null ? _getNamedGroups(match) : null;
},
'match': (String input) {
return input.split(regExp);
},
'search': (String input) => input.indexOf(regExp.toString()),
'replace': (String input, String replacement) =>
input.replaceAll(regExp, replacement),
'split': (String input) => input.split(regExp),
'matchAll': (String input) {
List<Map<String, String>> matches = [];
for (var match in regExp.allMatches(input)) {
matches.add(_getNamedGroups(match));
}
return matches;
},
};
}

Map<String, String> _getNamedGroups(RegExpMatch match) {
var namedGroups = <String, String>{};
for (var groupName in match.groupNames) {
namedGroups[groupName] = match.namedGroup(groupName)!;
}
return namedGroups;
}

@override
Map<String, Function> setters() {
// TODO: implement setters
throw UnimplementedError();
}

}
}