test.js 460 B

123456789101112131415161718
  1. const body = `# ほげ
  2. 検診 <- これは highlight
  3. <a href="検診">検診</a>
  4. [検診](検診) <- non highlight
  5. ほげ(検診)ほげ <- highlight
  6. ほげ[検診]ほげ <- highlight
  7. <a href="検診">検診</a> 検診 <- nhl hl hl
  8. `;
  9. const k = '検診'; //keyword.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
  10. const keywordExp = new RegExp(`(${k}(?!(.*?\]|.*?\\)|.*?"|.*?>)))`, 'ig');
  11. console.log(body.replace(keywordExp, '<em class="highlighted">$&</em>'));