Can this be done in Drafts? Remove {{ and move it down

So I have this the following text per document or file in drafts

In order to prevent bacteria from collecting under them and to deter scratching an important intervention for Impetigo infections is? {{c1:: fingernails short and clean}} 

Can a formula or script do the following

In order to prevent bacteria from collecting under them and to deter scratching an important intervention for Impetigo infections is? 

fingernails short and clean

So basically the script would remove those closed {{c:: }} from the selection and press enter twice, so it separates the text from the question.

Like this I would be able to push it to SRS application as simple flash card of Front and Back since the code url-callback is programmed to get the first part and treat it as the question and the 2nd part of bottom part as the answer.

Any insights would be greatly appreciated it.

JL

In the example, you have “c1::”, but in the following text you drop that. I’m going to assume your example is correct, and that it could be any character, but it is always exactly one character.

You may need to remove the period in the regular expression if your situation is different.

Rather than dealing with key presses, we deal with the text characters they produce. Here, we will work with newlines, represented by the “\n” token.

Here’s a script that will make a regular expression based substitution to replace the wrapping double braces and identifier (plus space), and prefix the wrapped text (represented by the $1 in the substitution, matching back to the capture group in the match regular expression that precedes it) with two new lines.

draft.content = draft.content.replace(/{{c.:: (.*?)}}/,"\n\n$1");
draft.update();

Hope that helps.

Thanks, I just tested with the sample

In order to prevent bacteria from collecting under them and to deter scratching an important intervention for Impetigo infections is? {{c1:: fingernails short and clean}} 

and it works beautifully. I just realized that sometimes I have no space after the {{c1:: so it would read as follows

This is the addition of 2 + 2 {{c1::equals 4}}

Can the script read for any amount of white space after the :: and before the next character, I suspected that at most, it will be two or three presses of the space bar but not more than that.

Thanks

Thanks @sylumer I tinkered with it by removing the space and now it accounts for any space after the delimeter ::

like this

draft.content = draft.content.replace(/{{c.::(.*?)}}/,"\n\n$1");
draft.update();

Thanks again.

Here’s a revised version.

  • It should remove any unnecessary whitespace after the double colons.you reg ex wi retain them at the start of the line, but I imagine that isn’t ideal
  • I’ve escaped the braces as they are special characters in reg ex and really should be escaped.
  • I’ve added a global flag in case you have multiple occurrences in a single draft.
draft.content = draft.content.replace(/\{\{c.::\s*(.*?)\}\}/g,"\n\n$1");
draft.update();

Thanks it works like a charm. Can you suggest a site where newbies can learn regular expressions?

Cheers
J

I like regex101 for trying them out. But, I just picked it up from a cheat sheet somewhere. You can go really deep on patterns and efficiency, especially when reading ahead and the like. So there are entire books on it. There are also different flavours to be aware of.

The Automators podcast did an intro episode that got a bit of praise. Because I know enough regex to do what I need to, I skipped it, and I don’t think it was really aimed at someone like me (I work in IT and have been a developer).

I suspect any tutorial will get you started.

Thanks I appreciate it !! and will have a listen.