I am struggling to construct the correct regex expression to replace markdown bold syntax (double stars) with LaTeX code using the stringr::str_replace_all() function.
For example, I have the string "**first chunk** not bold, and **bold again**", and I would like to convert it to "\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}"
Thank you
>Solution :
Use capture groups:
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}"