diff --git a/ChangeLog b/ChangeLog
index fee803ae804db7b62d0c8b8bfa37501a2ec5c7b2..d886a7e9efc12003efde0010882de942123ea858 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2016-03-19  Benno Schulenberg  <bensberg@justemail.net>
+	* src/search.c (search_init): Always remember the last typed string,
+	also when it's an invalid regex.  This fixes Savannah bug #47440.
+
 2016-03-17  Benno Schulenberg  <bensberg@justemail.net>
 	* src/search.c (do_research): Use the Search key bindings also during
 	a Re-search, to enable cancelling.  This fixes Savannah bug #47159.
diff --git a/src/search.c b/src/search.c
index 2855d86628bcae73dcfa2d25698b2e6a74554d22..6ceb9af0e0e58f099b78f4099f020f55c3d8bb24 100644
--- a/src/search.c
+++ b/src/search.c
@@ -206,10 +206,14 @@ int search_init(bool replacing, bool use_answer)
 
 	if (i == -2 || i == 0 ) {
 #ifdef HAVE_REGEX_H
-	    /* Use last_search if answer is an empty string, or
-	     * answer if it isn't. */
-	    if (ISSET(USE_REGEXP) && !regexp_init((i == -2) ?
-			last_search : answer))
+	    /* If an answer was given, remember it. */
+	    if (*answer != '\0') {
+		last_search = mallocstrcpy(last_search, answer);
+#ifndef DISABLE_HISTORIES
+		update_history(&search_history, answer);
+#endif
+	    }
+	    if (ISSET(USE_REGEXP) && !regexp_init(last_search))
 		return -1;
 #endif
 	    ;
@@ -431,18 +435,9 @@ void do_search(void)
     if (i != 0)
 	return;
 
-    /* If answer is now "", copy last_search into answer. */
+    /* If answer is empty, use what was last searched for. */
     if (*answer == '\0')
 	answer = mallocstrcpy(answer, last_search);
-    else
-	last_search = mallocstrcpy(last_search, answer);
-
-#ifndef DISABLE_HISTORIES
-    /* If answer is not "", add this search string to the search history
-     * list. */
-    if (answer[0] != '\0')
-	update_history(&search_history, answer);
-#endif
 
     findnextstr_wrap_reset();
     didfind = findnextstr(
@@ -850,15 +845,6 @@ void do_replace(void)
     if (i != 0)
 	return;
 
-    /* If answer is not "", add answer to the search history list and
-     * copy answer into last_search. */
-    if (answer[0] != '\0') {
-#ifndef DISABLE_HISTORIES
-	update_history(&search_history, answer);
-#endif
-	last_search = mallocstrcpy(last_search, answer);
-    }
-
     last_replace = mallocstrcpy(last_replace, "");
 
     i = do_prompt(FALSE,