I have some text, which contains a markdown link:
var text = 'some text some text [some text](link.md) some text some text';
I want to change that to
var newText = 'some text some text [some text](link.html) some text some text';
basically, changing the .md to .html, but only if it's a valid markdown link. A valid markdown link is []() with text between it.
Currently I have to following regex: /\[.*\]\(.*.md\)/g.
However, how will I perform a regex replace in Javascript (if the regex above matches, replace .md with .html)?
Try this replacement:
var text = '[some text](link.md)';
console.log("Before:\n" + text);
text = text.replace(/(\[[^\]]+\])\(([^\)]+).md\).*/, "$1($2.html)");
console.log("After:\n" + text);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With