重新格式化 json 数据中的字符

reformat characters in json data

我正在从 reddit json 检索数据。有些数据是这样的:

The actual resolution of this image is 3067x2276, not 4381x3251. See [this](https://www.reddit.com/r/EarthPorn/wiki/index#wiki_resolution.3F_what_is_that_and_how_can_i_find_it.3F) page for information on how to find out what the resolution of an image is.

我想将数据插入我页面上的 <p></p>,但 link 与上面的一样(不可点击)。

请注意,当我尝试在 Whosebug 上 post 它时,它会很好地重新格式化为可点击的 link。我该怎么做?

由 Whosebug 重新格式化:

这张图片的实际分辨率是3067x2276,不是4381x3251。有关如何查明图像分辨率的信息,请参阅 this 页面。

我该如何实现?

我觉得我被骗了,但是在我的浏览器中检查 OP,我得到...

<p>The actual resolution of this image is 3067x2276, not 4381x3251. See <a href="https://www.reddit.com/r/EarthPorn/wiki/index#wiki_resolution.3F_what_is_that_and_how_can_i_find_it.3F" rel="nofollow noreferrer">this</a> page for information on how to find out what the resolution of an image is.</p>

换句话说,如果您找到 [words](URL),请将其替换为:

<a href="URL">words</a>

这个小正则表达式试图捕获 [] 后跟 () 的内容。根据您期望的链接类型,检查 http 可能不够...

let regex = /\[(.*?)\]\(([^\)]+)\)/g;
let matches = regex.exec(line);
// matches ought to contains words and a potential url
if (matches.length > 2 && matches[2].startsWith("http://")) {
    // matches[2] is probably a url, so...
    let replace = `<a href="${matches[2]}">${matches[1]}</a>`
    // ...
}

从正则表达式开始,基本上是增强型通配符。 /\[.*\]\(.*\)/,虽然看起来很奇怪,但会发现 [*](*) 其中 * 可以是任意长度的字符串。所有这一切都可以找到这个出现的第一个索引。我试着看,但我不是 JS 的最佳人选。 https://www.w3schools.com/js/js_regexp.asp