react 替换评论中的表情包

·  阅读 11
const textToEmoji = (textStr = '', EmojisLibrary = []) => {
    const reg = /\[.*?\]/g
    const regArr = textStr.match(reg) || []
    const emojis = {}
    regArr.forEach(name => {
      EmojisLibrary.forEach(emojisItem => {
        if (`[${emojisItem.name}]` === name) {
          emojis[`[${emojisItem.name}]`] = `<img src=${emojisItem.url} width="20px" />`
        }
      })
    })
    textStr = textStr.replace(reg, function (a, b) {
      return emojis[a] || ''
    })
    return <div dangerouslySetInnerHTML={{ __html: content }}></div>
  }
`
复制代码
分类:
前端
标签:
收藏成功!
已添加到「」, 点击更改