从JSON对象值中检索URL

Retrieve only URL from JSON object value - Angularjs

本文关键字:检索 URL JSON 对象      更新时间:2023-09-26

我正在访问instagram API,以获取特定用户喜欢的照片和该用户发布的第一条评论。但是,我只想从文本中检索URL,例如,用户tellasaur第一条注释是:

"text":"Check out this link http://www.domain.com",

我只想检索http://www.domain.com。下面是我到目前为止的代码:

所以我想在这个部分应用一个正则表达式{{p.comments.data|getFirstCommentFrom:'tellasaur'}}"

但不确定如何。

JS

 angular.module('app',[])
    .filter('getFirstCommentFrom',function(){
      return function(arr, user){
        for(var i=0;i<arr.length;i++)
        {
          if(arr[i].from.username==user)
            return arr[i].text;
        }
        return '';
      }
    })
    .controller('TestCtrl', function($scope){
      $scope.pics = [
        { "images":{
                "low_resolution":{
                   "url":"https:'/'/scontent.cdninstagram.com'/hphotos-xaf1'/t51.2885-15'/s320x320'/e15'/11243658_841091872640638_1858051687_n.jpg",
                   "width":320,
                   "height":320
                },
             },
     "comments":{
                "count":38,
                "data":[
                   {
                      "created_time":"1436314585",
                      "text":"Check out this link http://www.domain.com",
                      "from":{
                         "username":"tellasaur",
                         "profile_picture":"https:'/'/igcdn-photos-b-a.akamaihd.net'/hphotos-ak-xfp1'/t51.2885-19'/11142181_1606991566225969_1204610350_a.jpg",
                         "id":"174270894",
                         "full_name":"kristella"
                      },
                      "id":"1024203434844916571"
                   },
                   {
                      "created_time":"1436317671",
                      "text":"Wow",
                      "from":{
                         "username":"sbcarol2002",
                         "profile_picture":"https:'/'/igcdn-photos-b-a.akamaihd.net'/hphotos-ak-xfp1'/t51.2885-19'/10707061_359756607505353_826681437_a.jpg",
                         "id":"1280059782",
                         "full_name":"Susan Long"
                      },
                      "id":"1024229322726738700"
                   },
                   {
                      "created_time":"1436320519",
                      "text":"'ud83d'udc93 dreamyy",
                      "from":{
                         "username":"veekster",
                         "profile_picture":"https:'/'/igcdn-photos-h-a.akamaihd.net'/hphotos-ak-xtf1'/t51.2885-19'/11117059_1743047859255223_204225114_a.jpg",
                         "id":"31179150",
                         "full_name":"Victoria Wright"
                      },
                      "id":"1024253210688915485"
                   }
                ]
             }
         }
      ]
    });

HTML NG-LOOP

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<div ng-app="app" ng-controller="TestCtrl">
  <li ng-repeat="p in pics">
    <a href="{{p.comments.data|getFirstCommentFrom:'tellasaur'}}" target="_blank"><img ng-src="{{p.images.low_resolution.url}}" /></a>
  <p></p>
  </li>
</div>

查看如何在文本中正则表达式匹配url的答案,您的解决方案在那里的某个地方