Regex for Obsidan links
up:: Obsidian
Order of matching
When applying transformations it is important to match in the order above:
- Images (
![[…]]
)- Link with alias (
[[…|…]]
)- Link without alias (
[[…]]
)Otherwise, the matches will be applied incorrectly. Images for example, match links without an alias as well.
All three in one:
const str = "[[link]] [[alias|link]] ![[image]]";
const mdReg = /(!)?\[\[(?:(.+?)\|)?(.+?)\]\]/g
const matches = str.matchAll(mdReg);
for (const match of matches) {
const isImage = !!match[0] // -> "!" or undefined
const alt = match[1] // -> "alt" or undefined
const link = match[2] // -> "link" or undefined
}