Using regexp to match only certain list elements

I have the following text in a note

something other text that is not matched by the regex

  • #task a task I don’t want
  • ? I want this line
  • #someothertask I also don’t want this task
  • ? I also want this line
  • $ this line I don’t care
  • some other text

I am using the code below to extract only the items in that list that start with “- ?”

const pages = dv.pages("#dvjs")

const regex = new RegExp(`- \? (.*)`, 'g')

for (const page of pages) {
    const file = app.vault.getAbstractFileByPath(page.file.path)
    const contents = await app.vault.read(file)
    const match = contents.match(regex)
    if (match) {
        dv.span(match)
    }
}

But it retrieves everything after the first hyphen it finds, I tested the regex online and there it only captured the two lines with the “- ?” at the beginning

image

I figured it out in the end… the correct regex is: - \\? (.*), had to use double \ to escape the ? symbol.

Why do you read the text manually when it’s available for dataview queries in the list context?

And why do you implement alternate checkboxes yourself, when you can style the task checkbox itself?

Given the latter the same query could be written as:

```dataview 
TASK
WHERE status = "?"
```

Because it is not a task, it’s a list callout

How do you even get those “list callouts” formatted?

Even with using those callout variations, I’d still consider using something like the following to avoid reading the entirety of the files:

```dataview 
LIST WITHOUT ID item.text
FROM #dvjs
FLATTEN file.list as item
WHERE startswith(item.text, "?")
```

But YMMV, so use what you’re comfortable and like. I just wanted to present some options, which I believe is slightly computational expensive.

1 Like

I probably should have clarified, the limitation of using DQL is that the FROM is not going to be a simple tag or folder, my use case requires a filter on the pages which I achieve using:

const pages = dv.pages("${dv.current().file.folder}")

This works for me because I am basically trying to extract from all the notes within a specific folder specifically formatted list callouts.

Hopefully this clarifies, and I appreciate very much the interest and follow up!

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