简体   繁体   English

如何在 React JS 中用多个 colors 突出显示多个文本

[英]how to highlight multiple text with multiple colors with in react JS

I want to make multiple texts with multiple colors like red, green, yellow something like that now it is working for only one color I need each text in a different color please help me here is the codehttps://codesandbox.io/s/quizzical-fire-qhu3hd?file=/src/App.js我想用多个 colors 制作多个文本,比如红色,绿色,黄色之类的东西现在它只适用于一种颜色我需要不同颜色的每个文本请帮助我这是代码https://codesandbox.io/s /quizzical-fire-qhu3hd?file=/src/App.js

import React from "react";

function App() {
  const highlightText = (text, textToMatch) => {
    const matchRegex = RegExp(textToMatch.join("|"), "ig");
    const matches = [...text.matchAll(matchRegex)];
    console.log(matches);
    return text.split(matchRegex).map((nonBoldText, index, arr) => (
      <div key={index}>
        {nonBoldText}
        {index + 1 !== arr.length && <mark>{matches[index]}</mark>}
      </div>
    ));
  };
  return (
    <h4>
      {highlightText(
        "The aim of this study was to someting  in vitro the potential of Aloe Vera juice as a skin permeation enhancer; worong text is added here to which Aloe Vera itself permeates the skin. Saturated solutions of caffeine, colchicine, mefenamic acid, oxybutynin, and quinine were prepared at 32 degrees C in Aloe Vera juice and water (control) and used to dose porcine ear skin",
        ["Aloe Vera", "and", "study"]
      )}
    </h4>
  );
}

export default App;

You need the array of colour.你需要颜色数组。 Please try this请试试这个

     const highlightText = (text, textToMatch) => {
        const matchRegex = RegExp(textToMatch.join("|"), "ig");
        const matches = [...text.matchAll(matchRegex)];
// this consist of the color array 
        const color = ["green", "red", "yellow"];
        return text.split(matchRegex).map((nonBoldText, index, arr) => (
          <div key={index}>
            {nonBoldText}
            {index + 1 !== arr.length && (
              <mark
                style={{
// and this match the color index and textMatchIndex
                  backgroundColor:
                    color[
                      matches[index] ? textToMatch.indexOf(matches[index][0]) : ""
                    ]
                }}
              >
                {matches[index]}
              </mark>
            )}
          </div>
        ));
      };

在此处输入图像描述

Can you please check below link, It have different class name based on this您能否查看以下链接,它有不同的 class 名称基于此

I need dynamic all this array values comes to from on handelChaneg from the input box i need fist word into red, the second word into green third into green ["Aloe Vera", "and", "study"]我需要动态所有这些数组值来自输入框的 handelChaneg 我需要第一个词变成红色,第二个词变成绿色,第三个词变成绿色 ["Aloe Vera", "and", "study"]

:https://codesandbox.io/s/charming-dirac-m3w8z2?file=/src/App.js :https://codesandbox.io/s/charming-dirac-m3w8z2?file=/src/App.js

You will find different class name and then you can define the css based on the colour class name.您会发现不同的 class 名称,然后您可以根据颜色 class 名称定义 css。

Please let me know if you have any question如果您有任何问题,请告诉我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM