从Javascript字符串中提取并保留域的URL



我需要提取属于https://twitter.com域,并将其存储为变量数组。我知道我在找RegEx(https?://(.+?.)?twitter.com(/[A-Za-z0-9-._~:/?#[]@!$&'()*+,;=]*)?)。我的问题是,我不知道是什么命令在JS中找到了这个,尽管我已经找过了

我的项目合作伙伴正在填充一个Google Sheets表,我将其作为HTML文件存储在本地,我在一个单独的HTML页面上提取并推送到控制台,如下所示。我的最终目标是将他放置在JS数组中多个列中的twitter配置文件的链接用于以后使用。

fetch('Directory.html').then(function (response) {
return response.text();
}).then(function (html) {
console.log(html);
}).catch(function (err) {
console.warn('Ooga booga.', err);
});

任何见解都值得赞赏。我爱这个社区,祝福你们所有人。

编辑

在下面的评论之后,我已经实现了这段代码,但Chromium控制台打印整个文档,就好像它什么都不过滤一样。为什么会这样?我最初尝试在regex内容前后不使用forwardslash/,但Chromium控制台抱怨出现了意外的:(冒号(标记。为什么会这样?

fetch('Directory.html').then(function (response) {
// The API call was successful!
return response.text();
}).then(function (html) {
// This is the HTML from our response as a text string
console.log(html);
}).catch(function (err) {
// There was an error
//  console.warn('Something went wrong.', err);
});
const paragraph = html;
const regex = /(https?://(.+?.)?twitter.com(/[A-Za-z0-9-._~:/?#[]@!$&'()*+,;=]*)?)/;
const found = paragraph.match(regex);
console.log(found);

在这里展示我自己的作品。非常感谢@Booboo。

fetch('Directory.html').then(function (response) {
return response.text();
}).then(function (html) {
const paragraph = html;
const regex = /(https?://(.+?.)?twitter.com(/[A-Za-z0-9-._~:/?#[]@!$&'()*+,;=]*)?)/g;
const found = paragraph.match(regex);
console.log(found);
});

我使用了一个名为csi.js的库来fetch一个外部HTML文档。

const paragraph = html可能是一条冗余线路。

CCD_ 4标识";https://twitter.com/"作为我想要的文本,使用g标志来获取字符串中的所有实例,而不是仅获取一个实例。

const found =行查找字符串中的匹配项。

console.log将结果打印到浏览器的控制台中。

最新更新