字符串中的jquery正则表达式(文本和图像)

时间:2011-06-19 23:16:12

标签: jquery regex json lightbox twitter-feed

我需要在jQuery中使用2件事(类似于Twitter)

  1. 正则表达式正常链接
  2. 检查链接是图像还是网站/ doc / etc(所以我可以使用灯箱或其他东西)。
  3. 例如,我有一个来自json对象的以下文本:

      

    你好我创造了这个   http://flicker.com/image然后我去了   http://www.google.com #home #alone

    1. 如果内容是图片,则必须检查链接
    2. 必须添加A元素。
    3. 标签'#'也必须链接。
    4. 我对jQuery有一点了解,我编辑/创建了以下脚本(可能不太好但是此时它正在运行)

      var username='yourname'; // set user name
      var format='json'; // set format, you really don't have an option on this one
      var url='http://api.twitter.com/1/statuses/user_timeline/'+username+'.'+format+'?callback=?'; // make the url
      var count = 0; // Start from 0
      var maxTweet = 10; // Maximum tweets (max limit is 15)
      $.getJSON(url,function(tweet){ // get the tweets
          $.each( tweet, function(i, l){
              var month1 = {}; // building my month array
              month1['Jan'] = '01';
              month1['Feb'] = '02';
              month1['Mar'] = '03';
              month1['Apr'] = '04';
              month1['May'] = '05';
              month1['Jun'] = '06';
              month1['Jul'] = '07';
              month1['Aug'] = '08';
              month1['Sep'] = '09';
              month1['Oct'] = '10';
              month1['Nov'] = '11';
              month1['Dec'] = '12';
              var d = tweet[count].created_at; // getting date
              var d = d.split(" "); // Splitting date
              var e = tweet[count].created_at.split(" "); // Splitting date
              var a = d[1]; // Month
              var month = month1[a]; // still month
              var d = d[5] + '-' + month + '-' + d[2] + 'T' + d[3] + 'Z' + d[4]; // date like 30-05-2011T13:45:45Z+1000
              var date = prettyDate(d); // using prettyDate founded on the Web
              if(count < maxTweet){
              if(date = 'undefined'){
                  var date = e[2] + '-' + month + '-' + e[5] + ' om ' + e[3];
              } // if it is out of range
          $("#twitter .slides_container").append('<p> <a href="http://twitter.com/#!/' + tweet[count].user.name + '" target="_blank"><img src="' + tweet[count].user.profile_image_url + '" alt="' + tweet[count].user.name + '" class="twitpic"></a> ' + tweet[count].text + '<br /><em>' + date + '</em></p>')
      }
          });
      count++;
      });
      

      tweet [count] .text =我需要正则表达式

      编辑(计数++;)

1 个答案:

答案 0 :(得分:0)

你可能会发现这样的内容很有用,可以链接你的推文。

html = tweet[count].text
    .replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@@#\/%?=~_|!:,.;]*[-A-Z0-9+&@@#\/%=~_|])/ig, '<a href="$1" target="_blank" rel="nofollow">$1</a>')
    .replace(/(?:^| )[@]+(\w+)/ig, ' @<a href="http://www.twitter.com/$1" class="user" target="_blank" rel="nofollow">$1</a>')
    .replace(/(?:^| )[#]+([\w\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u00ff\u0600-\u06ff]+)/ig, ' <a href="http://search.twitter.com/search?q=&tag=$1&lang=all" class="hash" target="_blank" rel="nofollow">#$1</a>');

不幸的是,除非您做出一些假设,例如链接是否包含.jpg等,否则您并不总是知道URL末尾的内容(这是实现灯箱功能所需要的)。