DD
DevDash

Regex for Markdown Bold

Regex Pattern

\*\*([^*]+)\*\*|__([^_]+)__

Markdown bold text (**text** or __text__)

Quick Answer

The regex pattern for markdown bold is `\*\*([^*]+)\*\*|__([^_]+)__`. Markdown bold text (**text** or __text__). This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.

Test Examples

InputResult
**bold text**✓ Matches
__also bold__✓ Matches
**word**✓ Matches
*italic*✗ No match
_single_✗ No match
not bold✗ No match

Code Examples

javascript

const regex = /\*\*([^*]+)\*\*|__([^_]+)__/;
const isValid = regex.test(value);

python

import re
pattern = r'\*\*([^*]+)\*\*|__([^_]+)__'
if re.match(pattern, value):
    print("valid")

ruby

pattern = /\*\*([^*]+)\*\*|__([^_]+)__/
if value =~ pattern
  puts "valid"
end

php

if (preg_match('/\*\*([^*]+)\*\*|__([^_]+)__/', $value)) {
    echo "valid";
}

java

String pattern = "\\*\\*([^*]+)\\*\\*|__([^_]+)__";
boolean isValid = value.matches(pattern);

Frequently Asked Questions

Related Regex Patterns

Want API access + no ads? Pro coming soon.