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
| Token | Description |
|---|---|
| \* | 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
| Input | Expected |
|---|---|
| **bold text** | Match |
| __also bold__ | Match |
| *italic* | No Match |
| **multiple** **words** | Match |
| not bold | No Match |
Try It — Interactive Tester
Match Highlighting(4 matches)
Matches & Capture Groups
23 charsFlags: gMatches: 4Ctrl+Shift+C to copy regex