Exclude sub tags of nested tags from search

Discord user Logiz’s way to search for only the parent segment of a nested tag:

(I’ve adjusted the formatting somewhat.)

solved searching only a tag and not its sub tags using regex …

note that each / needs a \ before it, like this \/.
(?!\S) at the end is to make sure the there is no further characters afterwards.

for the tag #a/b
you would paste this into the search bar
/#a\\/b(?!\S)/

this will exclude

  • #a/b/c
  • #a/b/d
  • #a/b/x (you get the point)

this would show notes with only this specific tag.

(Copied with permission. Source: Discord)

Things to note because this is a plain text search instead of a tag: search and thus doesn’t use the tag index:

  • Only matches hashtags, not tags in YAML frontmatter. It’s possible to match those, too, but it will complicate the search.
  • Will match things like URL fragments (https://example.com/page#part) if they are the same as the tag. It’s possible to avoid this with a more complicated search, but. It worth if it it doesn’t happen often.
  • It will be slower (tho Obsidian searches pretty quickly so this may not matter).

Further reading: Search - Obsidian Help

Alternate title for forum search: “Sesrxh only parent tag of nested tag”

1 Like