Dynamic Highlighting with Regular Expressions: How to use recursive queries?

I use the plugin that “dynamically highlights” certain things by capturing them via regex search terms. (I don’t know what flavor of regex this uses, though)

The case that I’m trying to improve: \\.+?\}

This captures everything between the backslash \ which starts a LaTeX command, and a close curly bracket }, meaning that something like \cite{abc} or \footnote{text} would be captured.

However, the reason I’d like to improve this, is that this does NOT capture the whole thing in cases such as \footnote{\cite{citekey1}; \cite{citekey2}.}, which is necessary when citing multiple sources in one footnote.

This captures everything until the first }, leaves out the semicolon and the space, and then captures the citekey and the first } but not the final period and final }.

Is it possible to capture everything including the last curly bracket?

I’ve played around in regexr.com and tried this: \\.+?(\}|(.+?)) in an attempt to capture everything before the final } but that just does the same thing as my previous query.

The problem is that threads and tutorials I’m finding seem to only use one instance of the character that it’s meant to filter for. Can I somehow tell it to capture everything before a } and after a \?

This seems to almost do what I want: (?<=[\\]).*(?=[\}]) but this excludes the first \ and the final }. How do I include those as well?

Because I need \color{abc} to have a different color, I need to exlude that. This was suggested on reddit: (\\(?!color)[^{}]+?\{((?1)|[^{}])+?\}) and this works perfectly in the online regex sandboxes, e.g. here.

Unfortunately this doesn’t work in the plugin. I don’t know why this is as I don’t know much about regex.

In the meantime I’m using this \\([abd-z]|ci).+?\} because at least that has the exception for \color

Any ideas?


JavaScript. The Help page on Search links to external documentation. Search - Obsidian Help

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