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

发布于 2024-11-16 02:22:03 字数 2376 浏览 5 评论 0原文

我需要 jQuery(类似 Twitter)

  1. 正则表达式中的两件事,一个正常的链接
  2. 检查链接是否是图像或网站/文档/等(这样我就可以使用灯箱或其他东西)。

例如,我有来自 json 对象的以下文本:

嗨,我创建了这个 http://flicker.com/image 我去了 http://www.google.com #home #alone

  1. 则必须检查链接
  2. 如果内容是图片, 必须添加一个元素。
  3. 主题标签“#”也必须链接。

我对 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 = 我需要

编辑正则表达式的位置(count++;)

I need need 2 things in jQuery (twitter like)

  1. regex a normal link
  2. check if the link is an image or an website/doc/etc (so i can use a lightbox or something).

for example i have the following text from a json object:

Hi i created this
http://flicker.com/image and i went to
http://www.google.com #home #alone

  1. the link must be checked if the content is an image
  2. the A element must be added.
  3. the hashtag '#' must also be linked.

I have a little knowledge about jQuery and i edited/created the following script (maybe not nice but at this moment it is working)

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 = where i need the regex

edited (count++;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心凉 2024-11-23 02:22:03

您可能会发现这样的东西很有用,可以链接您的推文。

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 末尾的内容(这是您实现灯箱功能所需的操作)。

You may find something like this useful, to link up your tweets.

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>');

Unfortunately you don't always know what's at the end of the URL without going there (which is what you would need to do to implement your lightbox feature) unless you make some assumptions such as if the link contains .jpg etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文