farling42 / obsidian-import-json

Plug-in for Obsidian.md which will create Notes from JSON files
MIT License
88 stars 5 forks source link

table helper and regex substitution #10

Closed AmbushXXVI closed 2 years ago

AmbushXXVI commented 2 years ago

I've been trying to work on some text replacement using regex in the body of an import, but I'm running into an issue. I've tried both of the below statements in my template, but neither of them works. They don't error, but the resulting text is not substituted. If I don't escape the single curly braces, I get an "incomplete quantifier" error in the console. Here is an example of the text as well as the regex find and substitute strings.

{{table this (toRegex "\{@spell.(.*?)\}") (toRegex "[[$1]]")}} {{table this "\{@spell.(.*?)\}" "[[$1]]"}}

Any help would be greatly appreciated and thanks again for the importer!

farling42 commented 2 years ago

The first parameter doesn't need toRegex. Internally the module generates a RegEx.

The expression MUST match the entire string for the match to be valid.

Internally the supplied match-string is surrounded with "^" at the start and "$" at the end to ensure that the entire string matches.

If yours are only partially matching then adding ".*" either side of your match-string should suffice to get a complete string match.

farling42 commented 2 years ago

I think you are trying to use the table helper to perform a general string pattern substitution function which it isn't designed for.

The table helper simply replaces the original "this" string with one of the supplied strings - in a look-up table style.

Unfortunately, the handlebars-helpers "{{replace x y z}}" function doesn't appear to support RegExp for y. I'm looking at providing such a function myself.

farling42 commented 2 years ago

0.14.0 contains a new "replacereg" helper which will do the job of {{replace}} but allow for regular expressions.

farling42 commented 2 years ago

You should use:

{{{replacereg item "{@spell.(.*?)}" "[[$1]]" }}}

(triple {{{ prevent escaping of any quote marks and other special characters which might be present in the original "this" string)

AmbushXXVI commented 2 years ago

Thanks farling, I had the realization sometime around 3am that I was maybe using it wrong in this instance. :(

And thanks also for the push in the right direction