Skip to content
Advertisement

How can I select expressions that cannot be a member of a group and are not empty in regex for markdown?

I’m trying to do a customized markdown. I am also using katex with the $ ... $ group. But when replacing expressions, if they are in the $ ... $ group, I have to not replace these expressions.

EXAMPLE : Lorem **Ipsum**(1) is *simply*(2) dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, $ sqrt{2} **must be no bold **(3) *must be no italic *(4) $ ****(5) **(6)…

  • Example (1): Start with ** and end with ** and it’s not in a $...$ group so it will be bold.
  • Example (2): Start with * and end with * and it’s not in a $...$ group so it will be italic.
  • Example (3): Start with ** and end with ** but it’s in a $...$ group so it will not be bold.
  • Example (4): Start with * and end with * but it’s in a $...$ group so it will not be italic.
  • Example (5): Start with ** and end with ** but it’s empty so it will not be bold.
  • Example (6): Start with * and end with * but it’s empty so it will not be italic.

So, I need two regex. One of them should select those that start with ** and end with ** that cannot be empty and are not in the $ ... $ group. The other is to select the ones that start with * and end with * that cannot be empty and are not in the $ ... $ group.

Advertisement

Answer

To select the ones that start with * and end with * that cannot be empty and are not in the $ ... $ group:

You may use this regex:

(?<!*)*[^*s]+*(?!*)(?=(?:(?:[^$]*$){2})*[^$]*$)

RegEx Demo

RegEx Details:

  • (?<!*): Negative lookbehind to fail the match if previous character is a *
  • *: Match opening *
  • [^*s]+: Match 1+ of any character that is not a * and not a whitespace
  • *: Match closing *
  • (?!*): Negative lookahead to fail the match if next character is a *
  • (?=(?:(?:[^$]*$){2})*[^$]*$): Lookahead to assert 0 or more pair of $..$ ahead to make sure we are not matching inside $...$

If you want to match single star text inside the $...$ use:

(?<!*)*[^*s]+*(?!*)(?!(?:(?:[^$]*$){2})*[^$]*$)

RegEx Demo 2

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement