Phl*_*das 3 regex case-insensitive dart
我正在使用dart 正则表达式并试图找到匹配项。
飞镖代码:http : //try.dartlang.org/s/SY1B
RegExp exp = const RegExp("/my/i");
String str = "Parse my string";
Iterable<Match> matches = exp.allMatches(str);
for (Match m in matches) {
String match = m.group(0);
print(match);
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试执行不区分大小写的搜索以查找字符串的所有匹配项。它说没有匹配项。我确定我在某个地方搞砸了,因为我是 regexp 的新手。如何修改代码以找到匹配项?
对于上下文,我计划修改代码以搜索我认为可以使用以下代码实现的任何字符串。
RegExp exp = const RegExp("/${searchTerm}/i");
Run Code Online (Sandbox Code Playgroud)
/pattern/flags 语法适用于 JavaScript,但不适用于 Dart,因为 Dart 没有正则表达式文字。相反,文档显示了这一点:
const RegExp(String pattern, [bool multiLine, bool ignoreCase])
Run Code Online (Sandbox Code Playgroud)
所以你的构造函数应该是这样的:
RegExp exp = const RegExp("my", ignoreCase: true);
Run Code Online (Sandbox Code Playgroud)