【问题标题】:Highlight separate keywords within a sentence在句子中突出显示单独的关键字
【发布时间】:2020-02-12 09:05:04
【问题描述】:

我有两个存储句子数组的状态,假设说sentencesHard state,另一个存储单词数组criticalWords state。我需要在sentenceHard 的句子中从criticalWords 中突出这些词。现在,如果句子中包含单词,它会突出显示整个句子,但我只需要突出显示该特定单词而不是整个句子。 例如,“Configuringreact-redux 是complicated

现在,我的代码如下所示:

states={
     criticalWords:["configuring", "complicated"],
     sentencesHard: ["configuring react-redux is complicated"]
}

{this.state.sentencesHard.map((sentence) => (
                <span style={{ backgroundColor: this.state.criticalWords.some(word => sentence.includes(word)) ? '#e4b9b9' : 'initial' }}>
                  {sentence}
                </span>
                ))
              }

【问题讨论】:

    标签: javascript reactjs


    【解决方案1】:

    在我看来,您的任务比您想象的要复杂一些,因为您需要妥善处理句子开头的大写关键字,而且您很可能希望保留原始标点符号。

    因此,对于我假设您使用的基于类的组件(尽管我更喜欢基于函数),您的用例可能看起来像:

    const { render } = ReactDOM
    
    const testKeywords = ['configuring', 'complicated'],
          testSentenses = ['Configuring react-redux, by far, is not complicated, whatsoever.']
          
    class Component extends React.Component {
      render(){
        return (
          <div>
          {
            this.props.sentenses.map(sentense => {
              const blocks = sentense.match(/(\w+|\W)/g)
              return blocks.map(block => 
                this.props.keywords.includes(block.toLowerCase()) ?
                <span style={{backgroundColor: 'grey',color:'white'}}>{block}</span> :
                block
              )
            })
          }
          </div>
        )
      }
    }
    
    render (
      <Component sentenses={testSentenses} keywords={testKeywords} />,
      document.getElementById('root')
    )
    &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.12.0/umd/react.production.min.js"&gt;&lt;/script&gt;&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.11.0/umd/react-dom.production.min.js"&gt;&lt;/script&gt;&lt;div id="root"&gt;&lt;/div&gt;

    【讨论】:

      【解决方案2】:

      您可以将句子拆分为单个单词:

      return (
        <>
          {this.state.sentencesHard.map((sentence) => (
            <span>
              {sentence.split(/(\W+)/g).map((word) => (
                <span style={{ backgroundColor: this.state.criticalWords.some(cw => word.includes(cw)) ? '#e4b9b9' : 'initial' }}>
                    {word}
                </span>
              )}
            </span>
          ))}
        </>
      )
      

      【讨论】:

      • 由于某种原因,它仍然突出显示整个句子
      【解决方案3】:

      为句子中的每个单词添加背景颜色。像这样

      {
        this.state.sentencesHard.map((sentence) => (
           <span>
            {
             sentence.split(' ').map(word => {
                return this.state.criticalWords.includes(word) ?
                           <span style={{backgroundColor: '#e4b9b9'}}>{word}</span> : word;
             })
            }
           </span>
        ))
      }
      

      【讨论】:

      • 它确实突出了单词,但是每个单词在句子中都是相互连接的,我的意思是单词之间没有空格。
      • @henrydoe,希望这有助于codesandbox.io/s/reactjs-change-state-tyfwg
      • @ManirajMurugan :一旦您在句子的开头找到关键字(首字母大写)或关键字右侧附有标点符号,该解决方案就会失败。查看解决上述问题的my solution。希望您喜欢它(如果喜欢,请点赞;)
      • @YevgenGorbunkov,我相信你的解决方案是一个更干净的解决方案.. 赞成.. 快乐编码!!!..
      猜你喜欢
      • 2011-08-22
      • 1970-01-01
      • 2013-04-07
      • 2017-10-18
      • 2012-02-10
      • 2017-10-04
      • 1970-01-01
      • 1970-01-01
      • 2016-06-16
      相关资源
      最近更新 更多