gpt4 book ai didi

javascript - 使用谷歌应用程序脚本从字符串中提取所有链接

转载 作者:行者123 更新时间:2023-12-02 13:55:43 24 4
gpt4 key购买 nike

我有一个字符串变量,其中包含链接(以及其他文本),并且我希望能够提取包含某个顾客的所有链接(例如包含单词“case”)...这可以吗?

变量字符串类似于:

var string = 'here is some text line among the ones there will be links like https://stackoverflow.com/questions/40725199/extract-all-links-from-a-string-with-google-app-script?noredirect=1#comment68679843_40725199 and more';

作为解决方法,我使用了此处描述的内容:extract links from document ,创建一个以字符串作为内容的文档,然后提取链接,但我想直接执行...

问候,

编辑(致鲁本):

如果我使用:

var string = 'http://mangafox.me/manga/tales_of_demons_and_gods/c105/1.html here is some text line among the ones there will be links like https://stackoverflow.com/questions/40725199/extract-all-links-from-a-string-with-google-app-script?noredirect=1#comment68679843_40725199 and more ';

我两次只获得第一个链接(参见屏幕截图 here )。

如果我使用:

var string = 'here is some text line among the ones there will be links like https://stackoverflow.com/questions/40725199/extract-all-links-from-a-string-with-google-app-script?noredirect=1#comment68679843_40725199 and more http://mangafox.me/manga/tales_of_demons_and_gods/c105/1.html ';

同样的情况(参见屏幕截图 here )。

最佳答案

Google Apps 脚本

function test2(){
var re = /\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'"".,<>?«»“”‘’]))/i;
var string = 'here is some text line among the ones there will be links like https://stackoverflow.com/questions/40725199/extract-all-links-from-a-string-with-google-app-script?noredirect=1#comment68679843_40725199 and more';
for(var i = 0; i <= re.exec(string).length; i++){
if(re.exec(string)[i]) Logger.log(re.exec(string)[i])
}
}

JavaScript。

var re = /\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'"".,<>?«»“”‘’]))/i;
var string = 'here is some text line among the ones there will be links like https://stackoverflow.com/questions/40725199/extract-all-links-from-a-string-with-google-app-script?noredirect=1#comment68679843_40725199 and more here is some text line among the ones there will be links like https://stackoverflow.com/questions/40725199/extract-all-links-from-a-string-with-google-app-script?noredirect=1#comment68679843_40725199 and more';
for(var i = 0; i <= re.exec(string).length; i++){
if(re.exec(string)[i]) console.log(re.exec(string)[i])
}

引用

RegularExpression to Extract Url For Javascript

关于javascript - 使用谷歌应用程序脚本从字符串中提取所有链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40725199/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com