news 2026/8/25 18:06:12

Vue3高亮文本(Highlight)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Vue3高亮文本(Highlight)

效果如下图:

在线预览

APIs

Highlight

参数说明类型默认值
text文本stringundefined
patterns需要高亮的文本内容string[][]
autoEscape自动转义。默认情况下,patterns中的元素会被转化为正则表达式进行匹配,这个过程中需要进行自动转义,正则表达式最终匹配的是元素的字面内容,例如\(匹配的就是\(。如果你需要Highlight组件去匹配使用patterns中元素本身构造的正则表达式,例如\(匹配的是(,则可以设为false。如果你看不懂这些,不要改这个设置。booleantrue
caseSensitive区分大小写booleanfalse
highlightTag高亮内容的HTML元素类型string‘mark’
highlightClass高亮内容的类名stringundefined
highlightStyle高亮内容的样式CSSProperties{}

创建高亮文本组件Highlight.vue

<script setup lang="ts">import{computed,h}from'vue'importtype{CSSProperties}from'vue'exportinterfaceProps{text?:string// 文本patterns?:string[]// 需要高亮的文本内容autoEscape?:boolean// 自动转义。默认情况下,patterns 中的元素会被转化为正则表达式进行匹配,这个过程中需要进行自动转义,正则表达式最终匹配的是元素的字面内容,例如 \( 匹配的就是 \(。如果你需要 n-highlight 组件去匹配使用 patterns 中元素本身构造的正则表达式,例如 \( 匹配的是 (,则可以设为 false。如果你看不懂这些,不要改这个设置。caseSensitive?:boolean// 区分大小写highlightTag?:string// 高亮内容的 HTML 元素类型highlightClass?:string// 高亮内容的类名highlightStyle?:CSSProperties// 高亮内容的样式}constprops=withDefaults(defineProps<Props>(),{text:undefined,patterns:()=>[],autoEscape:true,caseSensitive:false,highlightTag:'mark',highlightClass:undefined,highlightStyle:()=>({})})consttextOrVnodeChildren=computed(()=>{if(props.patterns.length===0||!props.text){return[props.text]}else{constpatternStr=props.patterns.map((word)=>(props.autoEscape?escapeRegExp(word):word)).join('|')constregex=newRegExp(`(${patternStr})`,props.caseSensitive?'g':'gi')constsplitItems=splitAndMarkByRegex(props.text,regex)returnsplitItems.map((item:{text:string;isMatch:boolean})=>{if(item.isMatch){returnh(props.highlightTag,{class:['highlight-mark',props.highlightClass],style:props.highlightStyle},item.text)}returnitem.text})}})functionescapeRegExp(text:string):string{returntext.replace(/[.*+?^${}()|[\]\\]/g,'\\$&')}functionsplitAndMarkByRegex(str:string,regex:RegExp):Array<{text:string;isMatch:boolean}>{constresult=[]letlastIndex=0letmatch// 在一个指定字符串中执行一个搜索匹配,返回一个结果数组或 nullwhile((match=regex.exec(str))!==null){if(match.index>lastIndex){result.push({text:str.slice(lastIndex,match.index),isMatch:false})}result.push({text:match[0],isMatch:true})lastIndex=regex.lastIndex// 针对字符串中所有可能的匹配项测试正则表达式,还是仅针对第一个匹配项if(!regex.global){break}}if(lastIndex<str.length){result.push({text:str.slice(lastIndex),isMatch:false})}returnresult}</script><template><spanclass="highlight-wrap"><template v-for="(textOrVnode, index) in textOrVnodeChildren":key="index"><template v-if="typeof textOrVnode === 'string'">{{textOrVnode}}</template><component v-else:is="textOrVnode"/></template></span></template>

在要使用的页面引入

<script setup lang="ts">importHighlightfrom'./Highlight.vue'import{ref}from'vue'consttext=ref('Vue Amazing UI 是一个 Vue3 的组件库,主题可调,全量使用 TypeScript 和 SFC,支持 tree shaking,有点意思')constpatterns=ref(['Vue Amazing UI','Vue3','TypeScript','SFC','tree shaking'])</script><template><div><h1>{{$route.name}}{{$route.meta.title}}</h1><h2class="mt30 mb10">基本使用</h2><Highlight:text="text":patterns="patterns"/><h2class="mt30 mb10">样式</h2><Highlight text="Vue Amazing UI 全量使用 TypeScript 编写,和你的 TypeScript 项目无缝衔接":patterns="['Vue Amazing UI', 'TypeScript']":highlight-style="{padding:'0 6px',borderRadius:'4px',display:'inline-block',color:'#fff',background:'#1677ff',transition:'all 0.3s cubic-bezier(0.4, 0, 0.2, 1)'}"/><h2class="mt30 mb10">区分大小写</h2><Highlight text="Vue Amazing UI 全量使用 TypeScript 编写,和你的 TypeScript 项目无缝衔接":patterns="['Vue Amazing UI', 'typeScript']"case-sensitive/><h2class="mt30 mb10">高亮标签</h2><Highlight:text="text":patterns="patterns"highlight-tag="span":highlight-style="{ fontWeight: '500', color: '#ff6900', textDecoration: 'underline' }"/></div></template>
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/25 18:02:39

Windows服务器等保2级加固实战:从身份鉴别到安全审计的完整指南

1. 项目概述&#xff1a;为什么Windows服务器加固是等保2级的必答题最近在帮几个客户做等保2级的合规整改&#xff0c;发现一个普遍现象&#xff1a;很多团队在安全建设上&#xff0c;对Linux服务器研究得头头是道&#xff0c;各种安全基线、入侵检测工具信手拈来&#xff0c;但…

作者头像 李华
网站建设 2026/8/25 18:01:58

软件测试面试全攻略:30道精选问题解析与实战技巧

1. 软件测试面试的核心价值与准备策略 在当前的IT就业市场中&#xff0c;软件测试岗位的竞争日趋激烈。根据行业调研数据显示&#xff0c;2023年测试岗位的平均面试通过率仅为18.7%&#xff0c;远低于开发岗位的27.3%。这组数据背后反映出一个关键事实&#xff1a;测试岗位的面…

作者头像 李华
网站建设 2026/8/25 17:52:34

软件测试面试实战指南:从理论到自动化框架

1. 软件测试面试全景指南作为从业十年的测试老兵&#xff0c;我见过太多候选人带着厚厚的"八股文"笔记来面试&#xff0c;却在实战环节频频翻车。这份清单不是简单的题库堆砌&#xff0c;而是结合行业真实需求的通关秘籍。从功能测试到自动化框架&#xff0c;从Linux…

作者头像 李华