A Find/Replace "Step" for Custom Actions (with RegEx)

Here you go: https://actions.getdrafts.com/a/1Fu

1 Like

I think this might be the sort of thing you are after.

// define regex to use...
//Drop the double quotes from the original and add a space after page
const findRegex = /\[page /g;
// define replacement expression...
const replaceWith = "#@";

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

//Do some more replacements...
draft.content = draft.content.replace(/^highlight #@/gm, "#@");
draft.content = draft.content.replace(/(#@[0-9]+)]: /g, "$1\n");

//Update the draft
draft.update;

Hope that helps.

1 Like

This is great - thank you both for your help with this!

Great - thanks for sharing!

Just as an addendum, please also write Readdle and ask them to offer better export options for annotations, including turning off or modifying the “highlight” text, and offering direct export to Drafts rather than having to copy and paste from an email draft… I’ve done this before but they don’t seem to think enough people care about it to bother changing anything.

1 Like

Will do - It’s really astonishing that there isn’t a more flexible way of dealing with annotations.

1 Like

Just thought you might be interested in some disappointing news - I have been using the beta of PDF Expert 7, and rather than improvements for those of exporting annotations, there are many steps backwards. The biggest is that they have taken away page numbers in the annotations export, basically making it useless for me for getting my highlights of readings into Drafts. Sigh.

Sorry to hear this. It would be nice if they at least let you customize this in the settings…

1 Like

So I got PDF Expert 7. The new HTML only annotation summary is annoying, but it was fairly trivial to make an iOS Shortcut that takes this file as input from the share menu, converts it to text, and then creates a new Draft. (I also added a step to remove line breaks because I don’t want them.) I’m left with an HTML file I don’t need, but in some ways it is actually easier than trying to do “select all” from an email message…

I am trying to figure out how to use Find & Replace with RegEx to manipulate text. In this case, want to set a variable using RegEx and then replace text with the variable.

Text:

Mr. John Smith

Mr. XX goes to town.

Script

// define regex to use...
const findRegex = /([M][r-s]\.\s)(\w*\s)(\w*)/g;
// define replacement expression...
const FirstName = "$2";
const LastName = "$3";

//Find Text
const XX = "XX";
//

// do the replacement...
draft.content = draft.content.replace(XX, LastName);
draft.update();

Result:

Mr. John Smith

Mr. $3 goes to town.

What am I doing wrong?

I would suggest posting this sort of thing in a new topic in future, but looking at you code, you set-up a regex string and never use it. Maybe swap it in for your xx variable in your replace?

Try this: RegEx Factory - Drafts Action

:nerd_face: