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?
source to share
We hope this is helpful. Here, we use a combination that can be possible **spaces words spaces**
, **words spaces**
and**spaces words**
Regex: (?<=\s|^)(?:\*\*)(\s+([^\*]+)\s+|\s+([^\*]+)|([^\*]+)\s+)(?:\*\*)
1. The
(?<=\s|^)
positive outlook beyond space and top of the line2.
(?:\*\*)
the match**
3.it
(\s+([^\*]+)\s+
will matchspace
then thensomewords
and thenspace
4.
\s+([^\*]+)
coincidencespaces
, then all to*
(not including*
)5.
([^\*]+)\s+)
coincidence with characters thenspaces
6.
(?:\*\*)
match**
source to share
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 ) )
source to share
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
source to share