Searching using Obsidian Query Language

Things I have tried

Trying to search the contents of my notes using Obsidian Query Language. I can return the titles of the notes just not the actual match.

What I’m trying to do

Trying to:

  • Search all of my notes or a folder
  • Return all text matches using regex within the notes
  • Results to be returned in an array or object using Javascript

Does anyway know if this is possible?

Are you looking to perform this search from within a document? If so, the following seems like it will meet your first two bullet points (I don’t know how vital the third was):

```query
section:/REGEX/
```

Using the Obsidian Query Language plugin, and the API of that you could do the following dataviewjs query, and get a link to all the files mention your search query.

```dataviewjs 
if ( 'obsidian-query-language' in this.app.plugins.plugins ) {
  const oql = this.app.plugins.plugins['obsidian-query-language']
  
  // Setup and execute search
  const query = "'M01L01"
  const searchResults = await oql.search(query, { template: "list" } )
  
  // Filter away files _starting_ with the query
  const filteredList = searchResults
    .filter(t => !t.path.split("/").pop().startsWith(query.substring(1)))
    .map(t => t.path)
  
  dv.list(filteredList)
}
```

I’ve set this up to remove files starting with the query, so that a file name “M01L01”, wouldn’t show up in the list. If you want those included, then remove the .filter line.

Similar code should also be possible to do with an execution command of Templater, but this depends whether you want dynamic searching which would refresh itself every time you visit this file, or a static search the first time. If you want dynamic searches, then you could just add this query as is to your template, and change the query to something like const query = "'" + lessonNumber (or whatever you stored your key term in).

Thanks @holroy I’m running the code within the templater plugin and so I don’t think I have access to dataviewjs.

The query I’m trying to run is

const query = "'M01L01 - (.*)"

Trying to pullback the matches of what’s in the brackets, the description of each lesson.

I’m not interested in the filenames that the matches are within. Do you know how to do this?

You change your mind on what you want all the time, so I resign from trying to help. Sorry, but it is too tiresome trying to read your thoughts on what you really want.

Hi @holroy sorry for the confusion. Requirement is the same as my initial note.

  • Return all text matches using regex within the notes

Assume it shouldn’t make any difference between using Dataview or Templater should it?

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