“Not a valid javascript regular expression” error

Hi! I’m trying to figure out how to debug a “Not a valid javascript regular expression” I’m getting in a “Search and replace in a note” action. The regular expression is here:

##\sEvents[\s\S]*?##\sPlans

(I’m writing a shortcut to update a list of calendar events in a note, in the H2 section “Events”, which always comes before the H2 section “Plans”.)

The regex always gives me the error “Not a valid javascript regular expression” when run in this action, but when I try this regex in the standard apple supplied “Match Text” action, it works without a hitch.

Any ideas as to what I’m doing wrong? Thanks for any help anyone might supply.

Welcome to the forum, @consequently! Apple doesn’t use JS-style regular expressions. Assuming your note looks like this:

Something

## Events whatever
## Plans

My guess is you want to replace the two headlines with either nothing or something completely different, effectively remove them? Your regex doesn’t have any captures, so your replacement doesn’t know about anything found in the search pattern. If so, this is the regex you’re looking for:

/##\s+Events[\s\S]*?##\s+Plans/msi

JS regexes require boundaries (/) and can have optional flags (msi, see Regular expressions - JavaScript | MDN for what those mean.)

I’ve made your regex a bit less strict. \s means “one whitespace here”, where whitespace is a space or tab etc., while \s+ means “one or more whitespace here” which makes it work even if for some reason there are two spaces after ##.

Hope this helps, but if not — I’m here! :wink:

1 Like

Excellent! That worked perfectly.

Now I have a little shortcut that I can launch from Obsidian itself (either on iOS or the Mac) which populates my daily note with a list of events for the day—and which can repopulate the list (overwriting the old one) if needed.

Thanks for your help.

1 Like