Update filename within Markdown image link?

This has been frustrating me. I have some content here I want to change some markdown image references to be changed just to the filename.

For example:



Should be changed to:



And all other content unchanged.

This is the macro:

But it is doing nothing to the text, and I just cannot see what I've missed here. I'd really appreciate a fresh pair of eyes. Maybe the regex is OK, but there's something KM specific that I am missing here?

All help greatly appreciated!

This search expression works for me:


with the replacement being $1


1 Like

Note that I want it to cater for any file type, and specifically those served from the AWS prod-files-secure domain. In reality, the source file will have a mixture of png, gif, jpg, jpeg.

I appreciate that this can provide a solution for me (which I can adapt a little). But I would like to understand where I have gone wrong with my original attempt if at all possible.

For starters, that looks more like a sed expression than a KM regex -- KM doesn't have the wrapping slashes, doesn't use a trailing g for "global", etc. Check out the Regular Expressions wiki page.

And I think you can go a bit more specific with your pattern by anchoring both ends, since you want to find "stuff before the first . of a line" and "stuff between the last / and last . of a line". So I'd use something like:

Search: ^([^.]+)(.*)/([^.]+)(.*?)$
Replace: $1-eyes$2/$3-eyes$4

Did you try using regex101.com to analyse your search pattern? Well, if you didn't you should give it a go.

This is what it makes of mine: regex101: build, test, and debug regex

Then my expression would become:


The file name in both label and url ?

You can often simplify (or skip) the regular expression by using it in a split rather than a search and replace.

Here, for example, splitting the two halves of the MD link on ](,
and applying the same search and replace,

(perhaps pp.jpegpp-eyes.jpeg) to each half.

In an Execute JavaScript for Automation action, for example, possibly something like:

Updated filename in MD image link.kmmacros (3.6 KB)

Expand disclosure triangle to view JS source
return (() => {
    "use strict";

    const main = () =>
                x => x.replace("pp.jpeg", "pp-eyes.jpeg")

    // ------------ FROM AND TO MD IMAGE LINK ------------

    // fromMDImageLink :: MD String -> (String, URL)
    const fromMDImageLink = imageLink =>
            x => x.slice(2)
            x => x.slice(0, -1)

    // toMDImageLink :: (String, URL) -> MD String
    const toMDImageLink = ([label, url]) =>

    // --------------------- GENERIC ---------------------

    // bimap :: (a -> b) -> (c -> d) -> (a, c) -> (b, d)
    const bimap = f =>
    // Tuple instance of bimap.
    // A tuple of the application of f and g to the
    // first and second values respectively.
        g => tpl => [

    // both :: (a -> b) -> (a, a) -> (b, b)
    const both = f =>
    // A tuple obtained by separately
    // applying f to each of the two
    // values in the given tuple.
        ([a, b]) => [

    return main();