How to reverse engineer a markdown link into 2 variables: title and URL

Hello, I use Markdown links a lot (thank you @ComplexPoint).

To be inserted in a SimpleMind MindMap , the link cannot be pasted directly. It first has to be split into the title and the URL .

Example: Let's say I have the markdown link
The Best Task Management Apps for 2024
[The Best Task Management Apps for 2024](https://uk.pcmag.com/productivity-2/90672/the-best-to-do-list-apps-for-2020)

how would I split it into 2 variables:
1- variable markdown_title (The Best Task Management Apps for 2024) and
2- variable markdown_URL (The Best Task Management Apps for 2024) ?

thank you very much

Either I misunderstand, or this isn't too difficult. As you can see below, this seems to do what I think you said. It separates the title from the link.

The string you need is:

\[(.*)\]\((.*)\)
4 Likes

For a simpler pattern, you can:

  • split the string with: .split( "](" )

  • drop the initial "[" with .slice(1)

  • drop the final ")" with .slice(1, -1)

If you rejoin the two parts with .join("\n"), then you can refer to each part with Keyboard Maestro Variable Array indexes (one-based):

Parts of a Markdown Link (Variable Array).kmmacros (4.1 KB)

1 Like

For labelled (rather than indexed) references, like:

  • local_JSON.label
  • local_JSON.url

you could use Keyboard Maestro's very flexible and powerful %JSONValue% token, and write something like:

Parts of a Markdown Link (%JSONValue%).kmmacros (4.1 KB)

1 Like

And the simplest of all may just be a Keyboard Maestro Variable Array split on "("

Parts of a Markdown Link – Splitting on "(" with Variable Array.kmmacros (4.7 KB)

2 Likes

congratulations for your understanding of regex. An excellent solution. thank you very much !

thank you VERY much for all 3 solutions. Solution number 3 is within my reach, ie easier to understand and an opportunity to understand splitting variables into substrings.

It's a shame that, unlike arrays, you can't use negative indices to count backwards through a string's characters. So
image

...doesn't work :frowning:

2 Likes