4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Flutterでテキスト内のURLをチェックする方法

Last updated at Posted at 2020-03-19

ListViewでデータを表示する前にテキストの中にURLが含まれているかどうかのチェックをしてから、その結果に対しての表示を変更させるのに使いました。
調べた時に日本語の説明がなかったので短いですが備忘録として!

final urlRegExp = RegExp(
          r"((https?:www\.)|(https?:\/\/)|(www\.))[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9]{1,6}(\/[-a-zA-Z0-9()@:%_\+.~#?&\/=]*)?");
      final urlMatches = urlRegExp.allMatches(message);
      List<String> urls = urlMatches
          .map((urlMatch) =>
              message.substring(urlMatch.start, urlMatch.end))
          .toList();
      urls.forEach((value) {
        if (value != null) {
          // 処理
        } else {
          // 処理
        }
      });

参考文献

RegExp class
Flutter - Validating a string for URL

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?