Make Text Between Asterisks Bold

Make text between asterisks bold

A simple regex will do the trick:

$thenewtext = preg_replace('#\*{2}(.*?)\*{2}#', '<b>$1</b>', '**Hello World** of PHP');

Wrap text between two asterisks in HTML *bold*

This seems to work pretty well:

var str = "This is my string and *this text* should be wrapped with";

var updatedstr = String.Concat(
Regex.Split(str, @"\*")
.Select((p, i) => i % 2 == 0 ? p :
string.Concat("<strong>", p, "</strong>"))
.ToArray()
);

Markdown to convert double asterisks to bold text in javascript

Your regex is broken, for one thing. You probably want something more like:

/\*\*[A-z0-9]+\*\*/gi

The * is a special character in regular expressions. If you want to match against a literal *, then you need to escape it with \.

For instance: http://jsfiddle.net/2LAL4/22/

However, even with this change there's still a fair ways to go before you get to where you really want to be. For instance, your example will not work if the text area contains a mix of bold and non-bold text.



Related Topics



Leave a reply



Submit