Javascript 使用 markdown 转换 url,而不是转换成 url 数组
我有以下代码,我有一些带有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为它可以帮助你:
I think it can helps you: