Reputation: 6805
I have this text: test **test** **test** **test**
and I want to change **test**
to <b>test</b>
with this code:
var $text = 'test **test** **test** **test**';
$text = $text.replace(/\*\*[^\**]\*\*/,'<b>test</b>');
but i get this output: test **test<b>test</b>test** **test**
note: between to stars can be anything. (like SO editor)
desired output: test <b>test</b> <b>test</b> <b>test</b>
what is the problem ?
Upvotes: 0
Views: 70
Reputation: 73024
Use this:
\*\*([^\*]+)\*\*
And replace with capture group $1
like so: http://regex101.com/r/dJ7zG2
Also, you have no reason to use $ in javascript variables like that.
var text = "test **test** **test2** **somethingelse**";
text = text.replace(\*\*([^\*]+)\*\*, "<b>$1</b>");
Result:
"test <b>test</b> <b>test2</b> <b>somethingelse</b>"
Upvotes: 2
Reputation: 5337
this works:
/\*\*[^\*]*\*\*/g
g
to replace all occurencesjsfiddle: http://jsfiddle.net/fDM3h/1/
code for replacing any content between **:
$text = $text.replace(/\*\*([^\*]*)\*\*/g,'<b>$1</b>');
Upvotes: 4