fork download
  1. ## Escaping function
  2. regex.escape <- function(string) {
  3. gsub("([][{}()+*^$|\\\\?.])", "\\\\\\1", string)
  4. }
  5. fun <- function(text, search) {
  6. gsub(paste0("(?!\\B\\w)(", regex.escape(search), ")(?<!\\w\\B)"), "<mark>\\1</mark>",
  7. text, ignore.case = TRUE, perl=TRUE)
  8. }
  9. fun("this is a test.", ".")
  10. # [1] "this is a test<mark>.</mark>"
  11.  
  12. fun("(this is a test)", ")")
  13. # [1] "(this is a test<mark>)</mark>"
Success #stdin #stdout 0.25s 39936KB
stdin
Standard input is empty
stdout
[1] "this is a test<mark>.</mark>"
[1] "(this is a test<mark>)</mark>"