Bug: Find and Replace by field inserts garbage data

When the find field is left empty some (but usually not all) of the selected cards have random data inserted.

An example of my Find and Replace:

This is sometimes the result of the action: (note the Track field)

The problem doesn’t occur when selecting “Treat input as regular expression“ with the * pattern.

So we can better understand the problem, please start by isolating it. (e.g. providing a 1 card deck that demonstrates the problem, along with associated instructions)

I tried but was unable to reproduce the OP’s issue.

However, there are 3 unusual behaviors that I saw with Anki desktop Find/Replace:

  1. The behavior of most other Find/Replace functionality that I’ve seen in other apps is that an empty Find field matches only empty fields. (Basically, the equivalent of matching ‘^$’, or more accurately, matching ‘^()$’, with the Replace operation replacing the empty match group ‘()’.)

However, in Anki it appears that an empty Find field matches ‘[^$.]()[^$.]’, replacing the empty match group ‘()’.

Some testing with Find: ‘’ (empty field), Replace: ‘x

Before, After:
‘’, ‘x
a’, ‘xax
a’, ‘x xax
'a ', ‘xax x
a’, ‘x xax x
ab’, ‘xaxbx
a b’, ‘xax xbx
a b’, ‘xax x xbx
a b’ (2 spaces between a and b), ‘xax x x xbx

  1. It is possible to enter space characters into a Note field in 2 ways:
    a. HTML editor
    b. Search and replace

Normally, when you enter space characters in a field, Anki converts them into ‘ ’. However, if you use either of the 2 methods above, you can enter space characters into a field.

When a field contains only space characters (and the HTML editor UI is hidden), the field appears to be empty. Users can be tricked into thinking a field is empty, when it actually isn’t. (Basically, Anki is hiding leading and trailing spaces.)

  1. These two behaviors interact with each other when you intend to find and replace a blank field, the field looks blank, but it actually contains space characters.

Before, After:

’ (1 space character), ‘x x
’ (2 space characters), ‘x x x
’ (3 space characters), ‘x x x x


I was able to produce the field value shown in the OP’s post by this sequence:

  1. Starting with an entirely empty field.
    Field: ‘’ (empty field)
    HTML editor: ‘’ (empty field)

  2. Find: ‘’ Replace: ‘2 1 7
    Field: ‘2 1 7
    HTML editor: ‘2 1 7

  3. Find: ’ ’ Replace ‘2 2 2
    Field: ‘2 2 2 2 2 2 2 1 2 2 2 7 2 2 2
    HTML editor: ’ 2 2 2 2 2 2 2 1 2 2 2 7 2 2 2

Are you proposing we change the behaviour of the empty, non-regex case? Anyone else want to chime in with their thoughts?

I haven’t been able to reproduce the original case either. It seems reasonable to wonder what was in the underlying HTML of the field before the action, and whether the Find was truly empty. Without knowing that, it might be impossible to reproduce.

It seems like the issue here isn’t that Find-and-Replace is behaving incorrectly, but that if you’re not quite careful with your Find, your Replace, and the current contents of your field – you might get unexpected results. I’m not sure how you can avoid this with a Visual editor that allows you to Find-and-Replace on the HTML.

I just took another look at this, and all of the cards that were affected by this weren’t actually empty like I originally thought. They contained full-width japanese numbers, and then I did a find and replace on that field using an empty string as the query.

In fact there is no bug. In the example in the original post 222222212227222 was just a find and replace, empty with 222 and the Track field being set to 217. Inserting 222 at every blank space of 217 produces the value 222222212227222.

1 Like

I’m glad to hear that the issue is resolved and there is no bug.

I’ve been thinking about the empty search behavior, and I now believe that I misremembered the behavior of other apps. I think that I now recall that most apps do not allow you to search with an empty field. Search functionality is disabled until a search term is entered into the Find field.

I’m not advocating for changes to Anki behavior, but if there were to be a change, I think that the new behavior might be that Find is disabled with an empty Find field for non-regex searches. (If users want to search for empty fields, they would need to use a regex search for ^$.)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.