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

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

问题描述

假设我将tweet作为字符串存储在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开头并以空格(或tweet结尾)结尾的任何内容都是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/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中,注意实体对象.

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天全站免登陆