You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
aku_new_community/lib/utils/link_text_parase.dart

24 lines
797 B

class LinkTextParase {
static List<String> stringParase(String text, {Pattern? pattern}) {
List<String> _listString = [];
List<String> _urls = []; //匹配到的url存入这个数组
//使用正则表达式匹配url将字符串中url前后的部分分割存储为字符串列表
Pattern _pattern = pattern ??
RegExp(
r"(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]"); //url的正则表达式
_listString = text.split(_pattern);
var _matches = _pattern.allMatches(text);
for (var item in _matches) {
_urls.add(item[0]!);
print(item[0]);
}
//将url插入字符串数组
for (var i = 0; i < _urls.length; i++) {
_listString.insert(2 * i + 1, _urls[i]);
}
return _listString;
}
}