Find sheet pairs of stars

I need to find a poorly formed markdown that breaks the separate markup parser. I'm looking for a RegEx that will find cases where it is **bold text**

incorrectly filled with a space before or after asterisks. For example ** this is wrong**

and **this is also **

as it is ** this one too **

, but **this is correct**

. Naturally, strings can often contain both. For example, here are some test cases:

This line is **correct** and **properly marked down**.
But this line **is not** because ** the second bold is wrong**.
** Also** this line is wrong **even though this is right**.

      

I've tried (?:\*\*[^\*]*\s\*\*)

but it actually captures the parts in between (the parts not enclosed in asterisks). Any advice?

+3


source to share


3 answers


We hope this is helpful. Here, we use a combination that can be possible **spaces words spaces**

, **words spaces**

and**spaces words**

Demo Regex



Regex: (?<=\s|^)(?:\*\*)(\s+([^\*]+)\s+|\s+([^\*]+)|([^\*]+)\s+)(?:\*\*)

1. The (?<=\s|^)

positive outlook beyond space and top of the line

2. (?:\*\*)

the match**

3.it (\s+([^\*]+)\s+

will match space

then then somewords

and thenspace

4. \s+([^\*]+)

coincidence spaces

, then all to *

(not including *

)

5. ([^\*]+)\s+)

coincidence with characters thenspaces

6. (?:\*\*)

match**

+2


source


Try the following:

(?:\*\*\S.+?\S\*\*|(\*\*(?:\s.+?|.+?\s)\*\*))

      

Explanation:



                                      // Line must contain:
(?: \ * \ * \ S. +? \ S \ * \ * | // Correctly formatted block OR
           (// block with BAD formatting - space
            \ * \ * (?: \ s. +? | // to the left of formatting OR
                         . +? \ s) \ * \ * // to the right of formatting
           )
)

Demo

+1


source


This is a javascript-friendly template:

^[^*]*(?:\*{2}[^ ][^*]*[^ ]\*{2}[^*\n]*)*(\*{2}(?: [^*]+|[^*]+ )\*{2}).*$

Explanation (in random terms):

^[^*]*                                # match anything preceding the first *
(?:\*{2}[^ ][^*]*[^ ]\*{2}[^*\n]*)*   # match zero or more valid sets of **'s, so we don't lose track of what is an opener/closer
(?:\*{2}(?: [^*]+|[^*]+ )\*{2})       # REQUIRE just one invalid ** set
.*$                                   # after one invalid set is found, it doesn't matter what trails it before end of line

      

Demo link Regex

0


source







All Articles