【发布时间】:2021-08-19 02:30:10
【问题描述】:
我需要从 HTML 代码的 JS 字符串中提取属于 https://twitter.com 域的 URL,并将它们存储为变量数组。我知道我正在寻找 RegEx (https?:\/\/(.+?\.)?twitter\.com(\/[A-Za-z0-9\-\._~:\/\?#\[\]@!$&'\(\)\*\+,;\=]*)?)。我的问题是我不知道是什么命令在 JS 中找到了这个,虽然我已经找过了。
我的项目合作伙伴正在填充我在本地存储为 HTML 文件的 Google Sheets 表格,我在单独的 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 控制台会打印整个文档,就好像它没有过滤任何内容一样。为什么是这样? 我最初尝试在正则表达式内容之前和之后没有正斜杠 / ,但 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);
【问题讨论】:
标签: javascript regex