gpt4 book ai didi

javascript - 正则表达式用字符串中的html标签替换字符

转载 作者:行者123 更新时间:2023-12-02 23:58:52 25 4
gpt4 key购买 nike

我收到了需要用输入标记替换的正则表达式单词。但是当我添加 html 时,它读取为纯字符串。
这是我的字符串:“你好,我的名字是/# Ann #/。我为/# IStaff#/工作,你能给我回电话吗”。我哪里做错了?

editModalText() {
const { modalMessage } = this.state
let regex = /\/#(.*?)#\//g
let replaced = modalMessage.replace(regex, '<input type="text">')
return replaced
}

<div>{this.editModalText()}</div>

最佳答案

这是因为它一个普通字符串。

如果您想将 JSX 语法混合到模板中,您将必须完全改变您的方法。

例如,构建 tokenizer这会将您的字符串转换为数组,例如:

[
"Hello, my name is ",
"/# Ann #/",
". I'm working for ",
"/# IStaff #/",
", could you please call me back"
]

然后循环它,并将原始字符串或适当的 JSX 元素插入新数组,最后返回该数组。

关于javascript - 正则表达式用字符串中的html标签替换字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55257803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com