Regex to Match Markdown Bold Text

Match bold text in Markdown syntax with this regex pattern. Captures both **double asterisk** and __double underscore__ bold formatting. Free tool.

Regular Expression

/\*\*(.+?)\*\*|__(.+?)__/g

Token Breakdown

TokenDescription
\*Matches a literal asterisk
\*Matches a literal asterisk
(Start of capturing group
.Matches any character except newline (unless dotAll flag is set)
+?Matches the preceding element one or more times (lazy/non-greedy)
)End of group
\*Matches a literal asterisk
\*Matches a literal asterisk
|Alternation — matches the expression before OR after the pipe
_Matches the literal character '_'
_Matches the literal character '_'
(Start of capturing group
.Matches any character except newline (unless dotAll flag is set)
+?Matches the preceding element one or more times (lazy/non-greedy)
)End of group
_Matches the literal character '_'
_Matches the literal character '_'

Detailed Explanation

This regex matches bold text in Markdown, supporting both the double asterisk and double underscore syntax. Here is the token-by-token breakdown:

** — Matches two literal asterisk characters. Asterisks are regex metacharacters (repetition quantifiers), so they must be escaped with backslashes.

(.+?) — A capturing group matching the bold text content. The dot matches any character except newlines, the + requires at least one character, and the ? makes the quantifier lazy (non-greedy). Lazy matching ensures the pattern stops at the first closing delimiter rather than consuming everything up to the last one.

** — Matches the closing pair of asterisks.

| — Alternation operator, providing the second syntax option.

__ — Matches two literal underscore characters. Underscores are not regex metacharacters, so no escaping is needed.

(.+?) — Another capturing group for the bold text content using the underscore syntax, with the same lazy matching behavior.

__ — Matches the closing pair of underscores.

The g flag enables global matching to find all bold text instances. The bold text content is captured in either group 1 (for asterisk syntax) or group 2 (for underscore syntax). This pattern is useful for Markdown parsing, preview generation, or converting Markdown to other formats. It handles basic bold text but does not account for edge cases like nested formatting or bold text spanning multiple lines.

Example Test Strings

InputExpected
**bold text**Match
__also bold__Match
*italic*No Match
**multiple** **words**Match
not boldNo Match

Try It — Interactive Tester

//g
gimsuy

Match Highlighting(4 matches)

**bold text** __also bold__ *italic* **multiple** **words** not bold

Matches & Capture Groups

#1**bold text**index 0
Group 1:bold text
Group 2:undefined
#2__also bold__index 14
Group 1:undefined
Group 2:also bold
#3**multiple**index 37
Group 1:multiple
Group 2:undefined
#4**words**index 50
Group 1:words
Group 2:undefined
Pattern: 23 charsFlags: gMatches: 4

Ctrl+Shift+C to copy regex

Customize this pattern →