求大佬正则匹配下面一段话里的特定字符转换成a链接。

Переехал_кирпич
  • 0
新手上路,请多包涵

ALK, anaplastic lymphoma kinase, is a receptor in the insulin receptor superfamily and is a key regulator of neuronal development (PMID: 21502284) and also promotes cell proliferation through activation of MAPK and PI3K signaling pathways (PMID: 27573755). Alk activating mutations, rearrangements, and fusions have been identified in various cancers (PMID: 22649787), including EML4-ALK in non-small cell lung cancer (PMID: 30108712, PMID: 30194140), and a number of mutations confer resistance in the context of Alk fusions (PMID: 25749034, PMID: 21948233).

转换成

ALK, anaplastic lymphoma kinase, is a receptor in the insulin receptor superfamily and is a key regulator of neuronal development (PMID: 21502284) and also promotes cell proliferation through activation of MAPK and PI3K signaling pathways (PMID: 27573755). Alk activating mutations, rearrangements, and fusions have been identified in various cancers (PMID: 22649787), including EML4-ALK in non-small cell lung cancer (PMID: 30108712, PMID: 30194140), and a number of mutations confer resistance in the context of Alk fusions (PMID: 25749034, PMID: 21948233).

自己的写法:react
image.png
这不是放在html里吗?为什么不显示a链接?而结果却是下面这样:
image.png

评论
阅读 270
2 个回答

这个时候是不是要说谢邀(?)

const t = s.replace(/\(PMID: (\d+)\)/g, (s, id) => `<a href="https://pubmed.ncbi.nlm.nih.gov/${id}/">${s}</a>`)
var str = `ALK, anaplastic lymphoma kinase, is a receptor in the insulin receptor superfamily and is a key regulator of neuronal development (PMID: 21502284) and also promotes cell proliferation through activation of MAPK and PI3K signaling pathways (PMID: 27573755). Alk activating mutations, rearrangements, and fusions have been identified in various cancers (PMID: 22649787), including EML4-ALK in non-small cell lung cancer (PMID: 30108712, PMID: 30194140), and a number of mutations confer resistance in the context of Alk fusions (PMID: 25749034, PMID: 21948233).`
var regone = /[\(|\)]/g
var regtwo = /PMID:\s*(\d+)/g
var n = str.replace(regone, '').replace(regtwo, (s, id) => `<a href="https://pubmed.ncbi.nlm.nih.gov/${id}/">${s}</a>`)
        document.querySelector('body').innerHTML = n

楼上的已经ok了,不过他的正则(PMID: 25749034, PMID: 21948233)这样没

有匹配出来。

点击不能跳转的原因是你的a标签必须是在html里面才可以!

撰写回答

登录后参与交流、获取后续更新提醒

宣传栏