用乳胶代码替换降价粗体语法

Replace markdown bold syntax with latex code

我正在努力构建正确的正则表达式以使用 stringr::str_replace_all() 函数将 Markdown 粗体语法(双星)替换为 LaTeX 代码。

例如,我有字符串 "**first chunk** not bold, and **bold again**",我想将其转换为 "\textbf{first chunk} not bold, and \textbf{bold again}"

stringr::str_replace_all(
  "**first chunk** not bold, and **bold again**",
  pattern = "\*\*.*\*\*", # this probably needs to be updated 
  replacement = <what goes here?>
)
#> "\textbf{first chunk} not bold, and \textbf{bold again}"

谢谢

使用捕获组:

library(stringr)

str_replace_all(
  "**first chunk** not bold, and **bold again**",
  pattern = "\*\*(.*?)\*\*",
  replacement = "\\textbf{\1}"
)

[1] "\textbf{first chunk} not bold, and \textbf{bold again}"