<p>I'm terrible with regex, but I've had a try and a Google (and even looked in reddit's source) and I'm still stuck so here goes:</p>

<p>My aim is to match the following 'codes' and replace them with the HTML tags. It's just the regex I'm stuck with.</p>

<pre><code>**bold text**
_italic text_
~hyperlink~
</code></pre>

<p>Here's my attempts at the bold one:</p>

<pre><code>^\*\*([.^\*]+)\*\*$
</code></pre>

<p>Why this isn't working? I'm using the preg syntax.</p>

use:
```
```
\*\*(.[^*]*)\*\*
```
```
explanation:
```
```
\*\*      // match two *'s
(.        // match any character
[^*]      // that is not a *
*)        // continuation of any character
\*\*      // match two *'s
```
```
in a character class "[ ]" "^" is only significant if it's the first character. so
```
(.*)
```
matches anything,
```
(.[^*]*)
```
is match anything until literal *
edit
: in response to comments to match asterisk within (ie
```
**bold *text**
```
), you'd have to use a non greedy match:
```
```
\*\*(.*?)\*\*
```
```
character classes are more efficient non greedy matches, but it's not possible to group within a character class (
see
"Parentheses and Backreferences...")