Avec l'extension `xstring` :
\documentclass{article}
\usepackage{xcolor}
\usepackage{xstring}
\NewDocumentCommand{\cmdFormat}{mmO{1}O{red}}{%
\IfSubStr[#3]{#1}{#2}{%
\IfSubStr[#3]{#1}{#2}{% test if string occures n times in the string
\StrBefore[#3]{#1}{#2}%
\textcolor{#4}{#2}%
\StrBehind[#3]{#1}{#2}%
}{%
#1
}%
}
\begin{document}
\cmdFormat{coucoucou}{cou}[2][red]
\cmdFormat{coucoucou}{cou}[3][green]
\cmdFormat{coucoucou}{cou}[4][cyan]
\cmdFormat{coucoucou}{cou}
\cmdFormat{fvghjwerjfh}{cou}
\end{document}
![alt text][1]
[1]: https://texnique.fr/osqa/upfiles/Screenshot_2021-11-20_at_13.31.56.png