"^^^foo-bar^^^foo-bar" ----> "^^^bar_foo^^^bar_foo" (replace-regexp-in-string "\\(foo\\)-\\(bar\\)" "\\2_\\1" "^^^foo-bar^^^foo-bar") ;; "^^^bar_foo^^^bar_foo"