Test if string.find outputs nil not working as expected

I am using the string.find function but finding an unexpected output when checking if it returns nil. However, it seems to work if I use string.match. I am new to Lua so just wondering if this is expected behaviour, and if so, why?

  • ${nil == nil} returns true
  • ${string.find("a", "b")} returns nil
  • ${string.match("a", "b")} returns `nil
  • ${string.match("a", "b") == nil} returns true
    but…
  • ${string.find("a", "b") == nil} returns false!

Am I doing something wrong?

For context, I am trying to write a query that finds all tasks containing a certain string but it just outputs all tasks instead:

${query[[
      from index.tag "task"
      where string.find(name, "instalment") ~= nil
  ]]}

However, the same query with string.match works fine:

${query[[
      from index.tag "task"
      where string.match(name, "instalment") ~= nil
  ]]}

Thanks in advance!

I just ran your examples in regular Lua and they should work as you expected. I think you found a bug in SilverBullet, can you open an issue on GitHub? Just copying your post would work, maybe you can include the link to this thread.

1 Like

This might fix it: Fix string.find by lochel · Pull Request #1390 · silverbulletmd/silverbullet · GitHub

1 Like