react搜索关键字(多关键字)高亮显示,补充当关键字中包含特殊字符时报错的解决

实现效果:需要高亮的文字为’降水‘和’分钟’
在这里插入图片描述

实现:

/**
 * 多关键字高亮显示
 */
import React from "react";

interface MultiKwHighLightProps {
  text: string; //需要显示的文本
  highLightWord: string[]; //需要高亮的字符串数组
  className?: string; //高亮内容的className
}

export default function MultiKwHighLight(props: MultiKwHighLightProps) {
  const { text, highLightWord, className, highLightStyle } = props;
  const highLightWords = highLightWord.filter(keyword => {
    return keyword !== "";
  });
  return text && highLightWord.length ? (
    <div>
      {text
        .split(
          new RegExp(
            `(?<=${highLightWords.join("|")})|(?=${highLightWords.join("|")})`
          )
        )
        .map(str => {
          if (highLightWords.includes(str)) {
            return (
              <span
                className={className ? className : undefined}
              >
                {str}
              </span>
            );
          } else {
            return str;
          }
        })}
    </div>
  ) : (
    <>{text || ""}</>
  );
}

补充:当需要显示高亮字符中包含特殊字符时(正则中的特殊字符如 (、)、*、+、-等)或报错,导致页面空白
下面是关键字为12(时的报错
在这里插入图片描述

解决:将字符中的特殊字符转化为+特殊字符,即在特殊字符加上转义符,参考js正则在特殊字符前进行转义并加上原来的字符

/**
 * 多关键字高亮显示
 */
import React from "react";

interface MultiKwHighLightProps {
  text: string; //需要显示的文本
  highLightWord: string[]; //需要高亮的字符串数组
  className?: string; //高亮内容的className
  highLightStyle?: React.CSSProperties; //高亮内容的style
}

export default function MultiKwHighLight(props: MultiKwHighLightProps) {
  const { text, highLightWord, className, highLightStyle } = props;
  const highLightWords = highLightWord.filter(keyword => {
    return keyword !== "";
  });
  const highLightWordsCopy = highLightWords.map(keyword => {
    // 字符中特殊符号的处理
    if (
      /(\+|\-|\&|\||\!|\(|\)|\{|\}|\[|\]|\^|\”|\~|\*|\?|\:|\\)/g.test(keyword)
    ) {
      //把匹配到的特殊字符替换成转义符,再加上原来的字符
      return keyword.replace(
        /(\+|\-|\&|\||\!|\(|\)|\{|\}|\[|\]|\^|\”|\~|\*|\?|\:|\\)/g,
        "\\" +
          keyword.match(
            /(\+|\-|\&|\||\!|\(|\)|\{|\}|\[|\]|\^|\”|\~|\*|\?|\:|\\)/g
          )[0]
      );
    }
    return keyword;
  });
  console.log(highLightWordsCopy);

  return text && highLightWord.length ? (
    <div>
      {text
        .split(
          new RegExp(
            `(?<=${highLightWordsCopy.join("|")})|(?=${highLightWordsCopy.join(
              "|"
            )})`
          )
        )
        .map(str => {
          if (highLightWords.includes(str)) {
            return (
              <span
                className={className ? className : undefined}
                style={
                  highLightStyle
                    ? highLightStyle
                    : !className
                    ? { color: "red" }
                    : {}
                }
              >
                {str}
              </span>
            );
          } else {
            return str;
          }
        })}
    </div>
  ) : (
    <>{text || ""}</>
  );
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值