从reddit评论中删除链接 [英] Removing the links from reddit comments
问题描述
我正在阅读reddit帖子下的评论.
I am reading the comments from under reddit posts.
有些评论包含链接,我希望删除它们.
Some of the comments have links, that I would like to get rid of.
示例(输入):
This is a [pic](https://i.imgur.com/yKmUMJD.jpg), [this](http://www.google.com) is a link
我希望字符串看起来(输出):
How I would like the string to look (output):
这是一张图片,这是一个链接
This is a pic, this is a link
我使用了此,以下代码可以解决问题:
I used this, and the following line does the trick:
item.data.body.replace(/ *\([^)]*\) */g, "").replace('[', '').replace(']', '');
我想知道如何在正则表达式中添加http
,因此它也不会删除普通"括号中的文本.
I would like to know how can I add http
to the regex, so it will not remove the "normal" bracket text as well.
谢谢
推荐答案
我看到您之前发布了一个类似的问题.现在,您还发布了您尝试过的内容……看起来您正在为此而苦苦挣扎.
I saw you had posted a similar question earlier. Now you have also posted what you have tried... looks like you are struggling with this one.
这就是我要做的:
const str = "This is a [pic](https://i.imgur.com/yKmUMJD.jpg), [this](http://www.google.com) is a link";
const tags = str.match(/\[.*?(?=\]\((.*?)\))/g).map(x => x.substring(1));
const newString = str.replace(/\[.*?\]\(.*?\)/g, () => {
return tags.shift();
});
console.log(newString)
第一步是查找所有标记的文本.换句话说,所有内容都包裹在[]
中.在上面的示例中,这将生成数组[pic, this]
.
First step is to find all of the marked up text. In other words, everything wrapped in []
. In your example above, this will yield the array [pic, this]
.
然后,我们需要用上面的每个匹配项替换整个网址bbcode(即[xxx](http://url)
).我们将数组视为一个队列,并在使用完后使用shift()
从数组中删除每个结果.
Then, we need to replace the whole url bbcode (i.e the [xxx](http://url)
) with each of our matches above. We treat our array as a queue, and remove each result from the array with shift()
once we've used it.
当然,此解决方案不是万无一失的.它不能处理任何()[]
字符都是标记或URL本身 的一部分的极端情况.
Of course, this solution isn't fool-proof. It won't handle the corner-case in which any of the characters ()[]
are part of either the markup or the URL itself.
这篇关于从reddit评论中删除链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!