Regex Issue with Bear Tags


I am having an issue with an action to parse tags from Bear in the format #test/test1/ which I am testing with a #test/test1 line in a draft.

My code which fails in the action is like this:

const re = /#[\/\w\d]+/g;
const masterTag = draft.content.match(re);
const findRegex = /\//g;
const replaceWith = "\n#";
const temp = masterTag.replace(findRegex, replaceWith);

This produces thiserror:
TypeError: masterTag.replace is not a function. (In ‘masterTag.replace(findRegex, replaceWith)’, ‘masterTag.replace’ is undefined)

When I show an alert with the masterTag variable it shows me it is indeed defined and is #test/test1. So I manually declared the masterTag variable to be also equal to #test/test1 and again it shows the same thing and not only that but with this manual declaration of masterTag the code runs OK.

So my question is what is the difference between the seemingly identical masterTags?

Thanks in advance.


It’s a type error. string.match() returns an array with all the matches for the regular expression.

masterTag is therefore an array, not a string.

You could alter the final line to read:

const temp = masterTag[0].replace(findRegex, replaceWith);

Which should work. (Or you could make the conversion elsewhere).

1 Like

Brilliant. Thanks so much for the answer so quickly!!

1 Like