I'd like to write something like this:
\replace{Text should be replaced here, here and here}{here}{Latex}
It should output
Text should be replaced Latex, Latex and Latex
How can this be done?
I'd like to write something like this:
\replace{Text should be replaced here, here and here}{here}{Latex}
It should output
Text should be replaced Latex, Latex and Latex
How can this be done?

\def\replace#1#2#3{%
\def\tmp##1#2{##1#3\tmp}%
\tmp#1\stopreplace#2\stopreplace}
\def\stopreplace#1\stopreplace{}
\replace{Text should be replaced here, here and here}{here}{Latex}
\bye
written as plain tex but would work in latex too.
-- (works great) but not ---?
– abukaj
Mar 21 '23 at 17:02
--- without removing the braces, am I right?
– abukaj
Mar 21 '23 at 17:34
This uses the higher-level macro StrSubstitute from xstring package. Use [0] as first optional parameter to replace all occurences of here, but as egreg stated in a comment, it will also replace in words like where or there
\documentclass{article}
\usepackage{xstring}
\begin{document}
\StrSubstitute[0]{Text should be replaced here, here and here}{here}{Latex}
\end{document}

A complex solution that only replaces complete words:
\documentclass{article}
%\usepackage{xparse,l3regex}% remove with recent LaTeX releases
\ExplSyntaxOn
\NewDocumentCommand{\replace}{mmm}
{
\marian_replace:nnn {#1} {#2} {#3}
}
\tl_new:N \l_marian_input_text_tl
\tl_new:N \l_marian_search_tl
\tl_new:N \l_marian_replace_tl
\cs_new_protected:Npn \marian_replace:nnn #1 #2 #3
{
\tl_set:Nn \l_marian_input_text_tl { #1 }
\tl_set:Nn \l_marian_search_tl { #2 }
\tl_set:Nn \l_marian_replace_tl { #3 }
\regex_replace_all:nnN { \b\u{l_marian_search_tl}\b } { \u{l_marian_replace_tl} } \l_marian_input_text_tl
\tl_use:N \l_marian_input_text_tl
}
\ExplSyntaxOff
\begin{document}
\replace{Text should be replaced here, here and here}{here}{\LaTeX{}}
\replace{Text should be replaced here, here and not there}{here}{\LaTeX{}}
\end{document}

A simpler solution that replaces all occurrences:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\replace}{mmm}
{
\marian_replace:nnn {#1} {#2} {#3}
}
\tl_new:N \l_marian_input_text_tl
\cs_new_protected:Npn \marian_replace:nnn #1 #2 #3
{
\tl_set:Nn \l_marian_input_text_tl { #1 }
\tl_replace_all:Nnn \l_marian_input_text_tl { #2 } { #3 }
\tl_use:N \l_marian_input_text_tl
}
\ExplSyntaxOff
\begin{document}
\replace{Text should be replaced here, here and here}{here}{\LaTeX{}}
\replace{Text should be replaced here, here and not there}{here}{\LaTeX{}}
\end{document}

\replace{\replace{start}{start}{fail}}{fail}{success}\\ \replace{fail,}{fail,}{success}
– Marian
Nov 25 '14 at 17:14
\replace so that the result of \replace{\replace{start}{start}{fail}}{fail}{success} is correct, viz., "success".
– Mico
Nov 25 '14 at 20:48
LaTeX Error: Filel3regex.sty' not found.. Proposed walkaround:\usepackage{xparse,expl3}` (from https://tex.stackexchange.com/a/491456/123888)
– abukaj
Mar 21 '23 at 15:02
xparse or l3regex is no longer necessary.
– egreg
Mar 21 '23 at 16:05
\replace{Mar.~19 -- Mar. 23}{--}{TO} result in Mar.~19 TO Mar. 23?
– abukaj
Mar 21 '23 at 16:35
Here's a LuaLaTeX-based solution. It uses Lua's powerful string.gsub function in the definition of the \replace macro. The macro \replace can be used recursively.

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{luacode} % for luacode environment and \luastring macro
%% Lua-side code
\begin{luacode}
function myreplace(s,a,b)
x = string.gsub(s,a,b)
tex.sprint ( x )
end
\end{luacode}
%% TeX-side code
\newcommand\replace[3]{\directlua{%
myreplace( \luastring{#1}, \luastring{#2}, \luastring{#3} ) }}
\begin{document}
\replace{Text should be replaced here, here and here.}{here}{Latex}
\replace{\replace{start}{start}{fail}}{fail}{success}
\replace{fail,}{fail,}{success}
\end{document}
Addendum to address the follow-up query by @user61681: If you must replace every single instance of "here" with "LaTeX" in the entire document, and if you can't do so by performing a global search-and-replace operation in your text editor, the following approach may be of interest. It "works" by (a) setting up a Lua function that employs the string.gsub function to replace all instances of "here" with "LaTeX" and (b) assigning this Lua function to LuaTeX's process_input_buffer callback, in effect making it act like a preprocessor on the input stream before TeX starts doing any of its usual work.
Note that this approach will fail is your document contains macros or labels that contain the string here.
% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{luacode}
\begin{luacode}
function myreplace ( s )
s = s:gsub ( "here" , "LaTeX" )
return s
end
\end{luacode}
\AtBeginDocument{\directlua{luatexbase.add_to_callback(
"process_input_buffer", myreplace , "myreplace" )}}
\begin{document}
Text should be replaced here, here and here.
\end{document}
OpTeX provides the macro \replstring\macro{from}{to}. For example:
\def\replace#1#2#3{\def\tmp{#1}\replstring\tmp{#2}{#3}\tmp}
\replace{Text should be replaced here, here and here}{here}{\OpTeX}
\replace{Text should be replaced here, here and not there}{here}{\OpTeX}
\bye
prints:
Text should be replaced OpTEX, OpTEX and OpTEX
Text should be replaced OpTEX, OpTEX and not tOpTEX
there? Shouldherebe replaced too, to givetLatex? – egreg Nov 25 '14 at 16:19