diff --git a/src/plugins/find/basetextfind.cpp b/src/plugins/find/basetextfind.cpp
index e567769d1e25d283b28cdf25eed1ef93299d0cf4..6f25776c4e2b1780a30661a85f6873527e547c4c 100644
--- a/src/plugins/find/basetextfind.cpp
+++ b/src/plugins/find/basetextfind.cpp
@@ -256,8 +256,9 @@ int BaseTextFind::replaceAll(const QString &before, const QString &after,
     regexp.setPatternSyntax(usesRegExp ? QRegExp::RegExp : QRegExp::FixedString);
     regexp.setCaseSensitivity((findFlags & Find::FindCaseSensitively) ? Qt::CaseSensitive : Qt::CaseInsensitive);
     QTextCursor found = findOne(regexp, editCursor, Find::textDocumentFlagsForFindFlags(findFlags));
-    while (!found.isNull() && found.selectionStart() < found.selectionEnd()
-            && inScope(found.selectionStart(), found.selectionEnd())) {
+    while (!found.isNull()
+           && (found.selectionStart() < found.selectionEnd() || after.length() > 0)
+           && inScope(found.selectionStart(), found.selectionEnd())) {
         ++count;
         editCursor.setPosition(found.selectionStart());
         editCursor.setPosition(found.selectionEnd(), QTextCursor::KeepAnchor);