I am trying to apply some regex to some text that is gathered with the Dataview plugin for the note program Obsidian.
I am using the dataview plugin to take some text from multiple notes and copying it as plain text, then I am taking that text and formatting it with regex. The issue I have is that the final output does not get formatted right and I do not know why.
Output example

The output is supposed to only have "- example" without the duplicate text and the brackets
const inputSentences = [
"- [[Note example 3.md|Note example 3]]",
"- [[Note example 2.md|Note example 2]]",
"- [[Note example 1.md|Note example 1]]"
]
function transformSentence(input) {
if (typeof input !== 'string') {
// Handle non-string input, e.g., return input as is or throw an error
return input;
}
const regex = /-\s*\[\[([^|]+)\|[^]]+\]\]/g;
return input.replace(regex, '- $1');
}
inputSentences.forEach(inputSentence => {
const outputSentence = transformSentence(inputSentence);
console.log(outputSentence);
})
You forgot to escape
]in your character set:It seems different regex flavors interpret
[^]]differently. In JavaScript, this is interpreted as\^\](^literal followed by a]), but in other flavors/languages like python or PCRE, it is interpreted as[^\]](any character other than]). It's better to escape characters like[,]or-within character sets manually to avoid ambiguities.Using mplungjan's minimal reproducible example: