This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function highlightText(root, keywords) { | |
| // 按长度降序,避免 "test" 抢先匹配掉 "testing" | |
| keywords = [...keywords].sort((a, b) => b.length - a.length); | |
| // 转义正则特殊字符 | |
| const escaped = keywords.map(k => | |
| k.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') | |
| ); | |
| const regex = new RegExp(`(${escaped.join('|')})`, 'gi'); |