Search code examples
elisp

Use `replace-regexp` in program rather in interative mdoe


I tried

ELISP> (replace-regexp "war" "negotiation" "trade war")
nil
ELISP> 

The results I expect is "trade negotiation"

How could use regex in elisp program?


Solution

  • I think you mistakenly used replace-regex instead of replace-regexp-in-string:

    ELISP> (replace-regexp-in-string "war" "negotiation" "trade war")
    "trade negotiation"
    

    replace-regexp's third argument is:

    Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace only matches surrounded by word boundaries. A negative prefix arg means replace backward.