Regex replace asterisk characters with html label

Does anyone have a good regex for this? For instance:

This is *an* example

should become

This is <b>an</b> example

I need to run this in Objective-C, but I can probably work as it should. This is a regular expression that gives me trouble (so rusty ...). Here is what I still have:

s/\*([0-9a-zA-Z ])\*/<b>$1<\/b>/g

But it does not seem to work. Any ideas? Thank:)

EDIT: Thanks for the answer :) If anyone is interested in how it looks in Objective-C using RegexKitLite:

NSString *textWithBoldTags = [inputText stringByReplacingOccurrencesOfRegex:@"\\*([0-9a-zA-Z ]+?)\\*" withString:@"<b>$1<\\/b>"];

EDIT AGAIN: Actually, to cover more characters for bold, I changed it to the following:

NSString *textWithBoldTags = [inputText stringByReplacingOccurrencesOfRegex:@"\\*([^\\*]+?)\\*" withString:@"<b>$1<\\/b>"];
+4
source share
4 answers

Only one character is compatible between *s. Try the following:

s/\*([0-9a-zA-Z ]*?)\*/<b>$1<\/b>/g

* s:

s/\*([0-9a-zA-Z ]+?)\*/<b>$1<\/b>/g
+5

\*[^*]+?\* <b>$1<\/b>?

+8

This one regex works for me (JavaScript)

x.match(/\B\*[^*]+\*\B/g)  
+1
source

I wrote a slightly more complex version that ensures that the asterisk is always on the border, so it ignores the dangling asterisk characters:

/\*([^\s][^\*]+?[^\s])\*/

Test phrases with which it works and does not work:

enter image description here

0
source

All Articles