newCloud/src/utils/highlightText.tsx
ylj20011123 a07144f2c3 update
2025-08-06 19:00:53 +08:00

18 lines
531 B
TypeScript

// 标识出搜索的文字
import React from 'react';
export const highlightText = (text: string, searchText: string) => {
if (!text || !searchText) return text;
const regex = new RegExp(`(${searchText})`, 'gi');
const parts = text.split(regex);
return (
<span>
{parts.map((part, i) =>
part.toLowerCase() === searchText.toLowerCase() ?
<span key={i} style={{ color: 'red' }}>{part}</span> :
part
)}
</span>
);
};