JavaScript-从JSON数组获取数据时,用锚标记替换字符串
我在执行对象数组上的循环时遇到了这个问题。考虑到我有以下对象数组。JavaScript-从JSON数组获取数据时,用锚标记替换字符串,javascript,html,reactjs,Javascript,Html,Reactjs,我在执行对象数组上的循环时遇到了这个问题。考虑到我有以下对象数组。 General : [ { question: 'Whats you suggestion?', answer: "Before you go ahead and book one of our trusted developer, please read our FAQ.", }, { question: 'Do you ha
General : [
{
question: 'Whats you suggestion?',
answer: "Before you go ahead and book one of our trusted developer, please read our FAQ.",
},
{
question: 'Do you have guaranteed solution?',
answer: 'Please let us know at hello@stackoverflow.com.',
}
]
我在我的JSX
(react应用程序)中使用了类似这样的map
General.map((faq, index) => (
<details>
<summary>{faq.question}</summary>
<div>
<Linkify>{faq.answer}</Linkify>
</div>
</details>
))}
General.map((常见问题解答,索引)=>(
{faq.问题}
{常见问题解答}
))}
现在我想要文本
hello@stackoverflow.com
呈现为html
锚定标记。我已经做完了。但是我们如何用纯javascript实现这一点呢 这适用于电子邮件,但您需要计算URL的正则表达式并链接它们。希望能有帮助
General.map((faq, index) => {
const emailRegex = /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/g;
const emailReplacer = "<a href='mailto:$1'>$1</a>";
return (
<details>
<summary>{faq.question}</summary>
<div>
<div>
{faq.answer.replace(emailRegex, emailReplacer)}
</div>
</div>
</details>
)
})
General.map((常见问题解答,索引)=>{
const emailRegex=/([a-zA-Z0-9.\u-]+@[a-zA-Z0-9.\u-]+\.[a-zA-Z0-9.\u-]+)/g;
const emailReplacer=“”;
返回(
{faq.问题}
{faq.answer.replace(emailRegex,emailReplacer)}
)
})
您可以自己尝试,如果尝试不成功,请寻求进一步帮助。堆栈溢出不存在,无法为您编写代码。@IanKemp相信我是这样做的,我尝试拆分sting,用所需的锚定标记替换它,但它将其视为字符串而不是纯文本。