Javascript 使用 markdown 转换 url,而不是转换成 url 数组

发布于 2025-01-11 09:43:16 字数 1653 浏览 0 评论 0原文

我有以下代码,我有一些带有 markdown 的 url(“a”和“b”),而另一些则没有(“c”和“d”),我必须确保将它们转换为字符串 url 数组,如下所示见下文。

我怎样才能提出一些建议?

let a = "a [this](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7e/T._rex_infection.png/440px-T._rex_infection.png)!"
let b = "b [this](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7e/T._rex_infection.png/440px-T._rex_infection.png)! - [this](https://upload.wikimedia.org/wikipedia/commons/thumb/a/a9/Mosasaurus-Skelett.jpg/440px-Mosasaurus-Skelett.jpg)"
let c = "https://upload.wikimedia.org/wikipedia/commons/thumb/7/7e/T._rex_infection.png/440px-T._rex_infection.png"
let d = ["https://upload.wikimedia.org/wikipedia/commons/thumb/7/7e/T._rex_infection.png/440px-T._rex_infection.png", "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a9/Mosasaurus-Skelett.jpg/440px-Mosasaurus-Skelett.jpg"]
let e = "['https://upload.wikimedia.org/wikipedia/commons/thumb/c/c9/Tyrannosaurus_rex_Sue_at_FMNH.jpg/440px-Tyrannosaurus_rex_Sue_at_FMNH.jpg']"
let f = "['https://upload.wikimedia.org/wikipedia/commons/thumb/c/c9/Tyrannosaurus_rex_Sue_at_FMNH.jpg/440px-Tyrannosaurus_rex_Sue_at_FMNH.jpg','https://upload.wikimedia.org/wikipedia/commons/thumb/b/bc/Mosasaurus_beaugei_34.JPG/440px-Mosasaurus_beaugei_34.JPG']"


const url = (text, autoMatch = false) => {
  if (!autoMatch) return [text].flat();
  if (autoMatch) {
    const match = text.match(/\((.*)\)/);
    console.log("*", match, ";")
    if (match === null) return "";
    return match[1] || "";
  }
};

console.log(url(a, true))
console.log(url(b, true))
//console.log(url(c))
//console.log(url(d))

I have the following code, I have some urls with markdown ("a" and "b") and others without ("c" and "d"), I have to make sure to turn them into an array of string url, as seen below.

How can I make some suggestions?

let a = "a [this](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7e/T._rex_infection.png/440px-T._rex_infection.png)!"
let b = "b [this](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7e/T._rex_infection.png/440px-T._rex_infection.png)! - [this](https://upload.wikimedia.org/wikipedia/commons/thumb/a/a9/Mosasaurus-Skelett.jpg/440px-Mosasaurus-Skelett.jpg)"
let c = "https://upload.wikimedia.org/wikipedia/commons/thumb/7/7e/T._rex_infection.png/440px-T._rex_infection.png"
let d = ["https://upload.wikimedia.org/wikipedia/commons/thumb/7/7e/T._rex_infection.png/440px-T._rex_infection.png", "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a9/Mosasaurus-Skelett.jpg/440px-Mosasaurus-Skelett.jpg"]
let e = "['https://upload.wikimedia.org/wikipedia/commons/thumb/c/c9/Tyrannosaurus_rex_Sue_at_FMNH.jpg/440px-Tyrannosaurus_rex_Sue_at_FMNH.jpg']"
let f = "['https://upload.wikimedia.org/wikipedia/commons/thumb/c/c9/Tyrannosaurus_rex_Sue_at_FMNH.jpg/440px-Tyrannosaurus_rex_Sue_at_FMNH.jpg','https://upload.wikimedia.org/wikipedia/commons/thumb/b/bc/Mosasaurus_beaugei_34.JPG/440px-Mosasaurus_beaugei_34.JPG']"


const url = (text, autoMatch = false) => {
  if (!autoMatch) return [text].flat();
  if (autoMatch) {
    const match = text.match(/\((.*)\)/);
    console.log("*", match, ";")
    if (match === null) return "";
    return match[1] || "";
  }
};

console.log(url(a, true))
console.log(url(b, true))
//console.log(url(c))
//console.log(url(d))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖伴 2025-01-18 09:43:16

我认为它可以帮助你:

const getLinksFromString = (text) => text.match(/https[^ '")]*/gi);

const getLinks = (data) => {
    switch (typeof data) {
        case 'string':
            return getLinksFromString(data);
        case 'object':
            const links = [];
            
            for (text of data) {
                links.push(...getLinksFromString(text));
            }
            
            return links;
    }
}

const data = "b [this](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7e/T._rex_infection.png/440px-T._rex_infection.png)! - [this](https://upload.wikimedia.org/wikipedia/commons/thumb/a/a9/Mosasaurus-Skelett.jpg/440px-Mosasaurus-Skelett.jpg)"

console.log(getLinks(data))

I think it can helps you:

const getLinksFromString = (text) => text.match(/https[^ '")]*/gi);

const getLinks = (data) => {
    switch (typeof data) {
        case 'string':
            return getLinksFromString(data);
        case 'object':
            const links = [];
            
            for (text of data) {
                links.push(...getLinksFromString(text));
            }
            
            return links;
    }
}

const data = "b [this](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7e/T._rex_infection.png/440px-T._rex_infection.png)! - [this](https://upload.wikimedia.org/wikipedia/commons/thumb/a/a9/Mosasaurus-Skelett.jpg/440px-Mosasaurus-Skelett.jpg)"

console.log(getLinks(data))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文