const body = `# ほげ 検診 <- これは highlight 検診 [検診](検診) <- non highlight ほげ(検診)ほげ <- highlight ほげ[検診]ほげ <- highlight 検診 検診 <- nhl hl hl `; const k = '検診'; //keyword.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); const keywordExp = new RegExp(`(${k}(?!(.*?\]|.*?\\)|.*?"|.*?>)))`, 'ig'); console.log(body.replace(keywordExp, '$&'));