实现高亮关键字鸿蒙示例代码

鸿蒙场景化示例代码技术工程师
发布于 2025-3-6 17:47
浏览
0收藏

本文原创发布在华为开发者社区

介绍

本项目通过RegExp正则匹配实现了高亮关键字功能。

实现高亮关键字源码链接

效果预览

实现高亮关键字鸿蒙示例代码-鸿蒙开发者社区

使用说明

安装完成后首先输入待匹配字符串,然后输入匹配字符串,可以发现下方显示的待匹配字符串中的匹配字符串高亮显示。

实现思路

  1. 使用正则表达式模式对字符串执行搜索。
while ((array = regex1.exec(str)) !== null) {
  indexList.push(array.index, regex1.lastIndex - 1)
}
  • 1.
  • 2.
  • 3.
  1. 区分高亮和非高亮区域,返回结果。
for (let index = 0; index < str.length; index++) {
  if (!indexList.includes(index)) {
    cache = cache + str[index]
    if (index > indexList[indexList.length-1] && index === str.length - 1) {
      result.push({ char: cache, isAim: false })
    }
  } else {
    result.push({ char: cache, isAim: false })
    result.push({ char: msg, isAim: true })
    index = index + msg.length - 1
    cache = ''
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

分类
收藏
回复
举报


回复
    相关推荐