Regular Expresesion Capture Group Positioning

I am working on an Action that is supposed to transform text in this format:

  • Mow the lawn

to this format:

  • [ ] #n/task Mow the lawn for {{ProjectName}} #s/scheduled

The regex script I’m using is:

const findRegex = /^- (.)*?/gm;
// define replacement expression...
const replaceWith = "- [ ]\#n/task $1 for \{{VacationName}} \#s/scheduled";

This works except that the $1 capture group is placed at the end of the reformatted task. Like this:

  • [ ] #n/task for {{ProjectName}} #s/scheduled Mow the lawn

What change do I need to make to the replacement expression to relocate the capture group to the proper position within the reformatted task, as opposed to its being moved to the end of the reformatted task?

try changing your regel to /^- (.*)/gm

you did just capture the „.“ in your group 1 where you wanted to grab the whole string until the end of the line.

the best website (i know) to test regex is regex101.com - if anything goes wrong I always check if the regex is correct there.

Note the “non-greedy” in the original. Relevant?

Thank you. That worked. I’ll spend more time using regex101.com in the future.

1 Like

yes sorry, missed this when I typed the regex again. doesn‘t make a difference in the example but include it if necessary :wink:

1 Like