Find & Replace multiple lines

I’m using an existing action to take any line that starts with * and turns it into a task in GoodTask.

I want to create a final script to this action to find all * and replace with - [x] so it doesn’t dupe the next time and I know what’s already been sent to GoodTask.

I currently have this but it only does one line. Looking for it to look for all * in the note and replace

// define regex to use…
const findRegex = “*”;

// define replacement expression…
const replaceWith = “- [x]”;

// do the replacement…
draft.content = draft.content.replace(findRegex, replaceWith)/gm;
draft.update();

Any help appreciated.

…mb

Try this.

// define regex to use…
const findRegex = /^\*/gm;

// define replacement expression…
const replaceWith = "- [x]";

// do the replacement…
draft.content = draft.content.replace(findRegex, replaceWith);
draft.update();

Worked perfectly. Thank you!

By the way, should the string to be searched for contain a space after the asterisk?

I’m not sure if Markdown treats an asterisk followed by a non-space - at the beginning of a line - as a bullet signifier.

1 Like

Strictly speaking, yes it should as it could update lines beginning with a bold or italic word, and the marked check would have a space after it. Also the bullet could be white space indented as well which you would presumably want to retain.

A more advanced version of the code to accommodate these points might then look something like this.

// define regex to use…
const findRegex = /^(\s*)\* /gm;

// define replacement expression…
const replaceWith = "$1- [x] ";

// do the replacement…
draft.content = draft.content.replace(findRegex, replaceWith);
draft.update();

This changes this

* Hello
    * World
    * How
*Are*
* You?

To this

- [x] Hello
    - [x] World
    - [x] How
*Are*
- [x] You?
1 Like

Thanks to you both! I’ll modify the script and test.

1 Like