In my work on JavaScript regex for markdown formatting, I am trying to match instances where a single underscore (_) or asterisk (*) occurs once (at the beginning, end, or surrounded by other characters or whitespace), as well as occurrences of three underscores (_ _ _) or asterisks (* * *)
Examples of successful matches would be a single asterisk (*) and three asterisks (* * *), while phrases like **a and **_ would not be considered matches.
The goal is to identify elements that are italic (*italic*
) or bold and italic (***bold italic***
) formatted in markdown. Other variations like _** italic bold **_
should also be matched.
I currently have some patterns in place, but they don't seem to be fully effective. If possible, I'd like to consolidate this into one regex pattern.
(^|[^_])_([^_]|$)
(^|[^_])_([^_]|$)
(^|[^\*])\*([^\*]|$)
(^|[^\*])\*\*\*([^\*]|$)
// EDIT
Matching examples for *****
*em*
***em***
__*em*
__*em*__
word*em*word
Non-matching examples for *****
**em**
****em****