Action to remove brackets and content between them

Hello, I’m not a programmer so apologies if this is a basic question.

Throughout a note, I would like to run a find and replace action that finds brackets and removes them and the content between them. For instances:

Find: [6,7], [12.13,17]
Replace: blank space

Example:
Before

… antiestrogen activity due to the presence of different components such as flavonoids and the isoflavan glabridin [18,19]. Licorice also has an antiandrogen effect by blocking 17 hy- droxysteroid dehydrogenase [19,20,23], but fat tissue does not possess this type enzyme.

After

… antiestrogen activity due to the presence of different components such as flavonoids and the isoflavan glabridin . Licorice also has an antiandrogen effect by blocking 17 hy- droxysteroid dehydrogenase , but fat tissue does not possess this type enzyme.

Is there a way to do this?
Thank you

1 Like

If you just need to do this ad-hoc, you can use find and replace with the “Use Regular Expression” option enable.

Regular expressions are a separate topic, but a find for \[.+?\] would find all the sets of brackets. Leave the replace field blank, and you could clear those out.

If you do want an action because you need to do this repeated on different texts, the script below would do the same thing to the current text in the editor:

// remove all brackets with text between them in the editor's text
editor.setText(editor.getText().replaceAll(/\[.+?\]/, ""));

Put this script in a “Script” action step in an action and you should be good to go.

Thank you. I insert that shirt code in the find and replace and it worked like a charm. I do use this regularly so that I’m keenly interested to turn this into an action. When I attempt to do this, I get this error message:

Script Error
Script Error: SyntaxError: Unexpected
token ‘.’. Expected ‘)’ to end an
argument list.
Line number: 2. Column undefined

STEPS I’M TAKING

  1. Add new Action
  2. Under Advanced, add Script
  3. In script editor, remove help language and paste your code:

// remove all brackets with text between them in the editor’s text
editor.setText(editor.getText().replaceAll(/[.+?]/, “”);

Then I get the error when I run it

Any suggestion?

Many thanks

My bad. Updated my example above. It was missing a matching close parentheses.

Thank you. Shoot! Still no luck.

Getting this message:

Script Error
Script Error: TypeError:
String.prototype.replaceAll argument
must not be a non-global regular
expression
Line number: 2. Column 43

Ugh. Sorry about that…I really should not trust myself to code JavaScript without testing it…this should be it:

editor.setText(editor.getText().replace(/[.+?]/g, “”));
1 Like

I am unable to get the action to work but I appreciate your time trying to help.

Try this action.

It’s as per Greg’s code, but I’ve changed the quotation characters and added a backslash before the first square bracket (to escape the character and stop it being treated like a match group):

editor.setText(editor.getText().replace(/\[.+?]/g, ""));

I tried it on the sample text and it seemed to match the expected output.

2 Likes

Ok, dang copy and paste removed the escaping. Obviously was not at my best trying to assist yesterday, apologies.

2 Likes

This worked beautifully! Thank you, I will use this daily in this workflow:

  • Important journal article pdf into Drafts.
  • Remove bracketed references.
  • Send to Matter app to listen to the article on a walk.

Much appreciated!

1 Like