18 lines
531 B
TypeScript
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>
|
|
);
|
|
}; |