如何使用 JavaScript RegEx 从推文中提取 URL? [英] How to extract a URL from a Tweet with a JavaScript RegEx?

查看:46
本文介绍了如何使用 JavaScript RegEx 从推文中提取 URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我将推文作为字符串存储在 JS 变量中...

Assuming that I have the tweet stored as a string in a JS variable...

如何使用 JavaScript RegEx 从推文中提取 URL?

How to extract a URL from a tweet with a JavaScript RegEx?

这应该比从字符串中提取 URL 容易得多,因为:

This should be much easier than extracting a URL from a string, because:

我假设任何以 http 或 www 开头并以空格(或推文结尾)结尾的都是一个 URL.

I will assume that anything that starts with http or www and ends with a blank space (or end of tweet) is a URL.

推荐答案

这是我用来从 Twitter 状态中提取链接的正则表达式之一.

Here is one of the regular expressions that I've used for pulling links from Twitter statuses.

链接匹配模式

(?:<w+.*?>|[^=!:'"/]|^)((?:https?://|www.)[-w]+(?:.[-w]+)*(?::d+)?(?:/(?:(?:[~w+%-]|(?:[,.;@:][^s$]))+)?)*(?:?[w+%&=.;:-]+)?(?:#[w-.]*)?)(?:p{P}|s|<|$)

或者,如果您控制如何从 Twitter 获取状态,您可以将 include_entities 参数传递给 statuses/show(或任何其他支持它的方法,例如 statuses/user_timeline) 让 Twitter 为您列出链接、提及和主题标签,如下所示:

Alternatively, if you control how the statuses are fetched from Twitter, you can pass the include_entities parameter to statuses/show (or any other method that supports it, such as statuses/user_timeline) to have Twitter break out the links, mentions, and hashtags for you, like the following:

http://api.twitter.com/1/statuses/show/23918022347456512.json?include_entities=true

在结果 JSON 中,注意 entities 对象.

In the resultant JSON, notice the entities object.

"entities":{"urls":[{"expanded_url":null,"indices":[27,53],"url":"http://tinyurl.com/38wp7nt"}],"hashtags":[],"user_mentions":[]}

现在,您可以引用从 Twitter 返回的数据,而不必自己解析.这种方法的最大优点是您可以将工作卸载到 Twitter,而不必担心您的正则表达式是否与 Twitter 的完全匹配.

Now, you can reference the data returned from Twitter rather than having to parse it yourself. The best things about this approach are you offload the work to Twitter, and never have to worry whether your regular expression will match with Twitter's exactly.

这篇关于如何使用 JavaScript RegEx 从推文中提取 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆