【发布时间】:2018-11-04 07:33:00
【问题描述】:
来自https://github.com/iansinnott/react-string-replace/issues/33的共享问题
反应字符串替换:https://github.com/iansinnott/react-string-replace
我在使用reactStringReplace 格式化带有粗体/斜体标签的文本时遇到问题。我的设置如下:
var text1 = "[b]Testing bold [i]and italic[/i] tags[/b]"
var text2 = "[b]Testing bold [i]and italic tags[/b][/i]"
let replaced = reactStringReplace(text1, /\[b\]([\s\S]+)\[\/b\]/g, (match, i) => {
return <b key={i}>{match}</b>
})
replaced = reactStringReplace(replaced, /\[i\]([\s\S]+)\[\/i\]/g, (match, i) => {
return <i key={i}>{match}</i>
})
// result (html)
<b>Testing [i]bold and italic[/i] tags</b>
<b>Testing [i]bold and italic tags</b>[/i]
我不确定这是reactStringReplace 的问题,还是我正在使用的正则表达式的问题,还是其他问题。如果我首先应用斜体替换,我会在我期望的位置得到斜体标签,但 [b] 标签保持不变。这个用例可以使用reactStringReplace 还是我需要使用dangerouslySetInnerHtml?
奖励:reactStringReplace 是否能够处理不平衡的标签,或者像 text2 那样格式不正确的标签,或者我应该做一些预处理以确保字符串正确平衡和格式化?
【问题讨论】:
-
到目前为止,还不能处理嵌套替换。我看到您正在开发一个补丁以将此功能添加到 reactStringReplace。但这可能超出了这个问题的范围。
标签: javascript regex reactjs