Is this for interactive use? I suppose so. As the doc string for
This function is for interactive use only; in Lisp code
use `re-search-forward' and `replace-match' instead.
Are you really asking about
replace-regexp, and not
Because if you use the latter then the answer is included in Isearch by default: Just use
C-M-% when you're regexp-isearching, and you immediately switch to
query-replace-regexp, using the search regexp as the regexp to search for with
query-replace-regexp. And of course with
query-replace-regexp you can always hit
!, to replace all subsequent matches.
But if you really want to use
replace-regexp explicitly then you can use Isearch+ to get the regexp you use with
C-M-s, to use it as the regexp to use with
replace-regexp. For that, you just use
M-w while isearching, to copy the current search pattern to the kill ring. And then use
M-x replace-regexp and use
C-y, to yank that regexp as the one to use for that command.
If you want this behavior and you don't want to load library Isearch+ then you can just use the code for it:
(defun isearchp-kill-ring-save () ; Bound to `M-w' in `isearch-mode-map'.
"Copy the current search string to the kill ring."
(let ((message-log-max nil)) (message "Copied search string as kill"))
(define-key isearch-mode-map "\M-w" 'isearchp-kill-ring-save)