findUrls method

List<String> findUrls(
  1. String text
)

The findUrls function takes a text parameter and uses a regular expression to find URLs within the text. It then extracts the matched URLs and adds them to a list, which is returned by the function.

Implementation

List<String> findUrls(String text) {
  final RegExp urlRegExp = RegExp(
    r'(http|https):\/\/[^\s/$.?#].[^\s]*',
    caseSensitive: false,
    multiLine: true,
  );

  final Iterable<Match> matches = urlRegExp.allMatches(text);

  final List<String> urls = [];

  for (Match match in matches) {
    urls.add(match.group(0)!);
  }

  return urls;
}