Let’s say I have a string like this:
=====
and I want to replace it with this:
-----
I only want to replace it if it has more than a certain number of that character (we’ll say > 3).
So, these should be the replacements:
=== -> ===
==== -> ----
===== -> -----
The application is I want to replace all level 1 heading marks in markdown with a level 2 mark, without changing embedded code blocks.
I know I can do this:
/=/-/g, but this matches anything with an equals sign (if (x == y)), which is undesirable.
or this:
/===+/----/g, but this doesn’t account for the length of the original matched string.
Is this possible?
It’s possible with Perl:
Flag /e makes Perl execute expression in second part of s///.
You may try this with oneliner: