Jquery replace square brackets

I am trying to convert square brackets in a submit form. I will not go into the form submission part as this is not required.

I had no problem replacing the newline with the <br /> tag as follows:

 new_line = message.replace(/\n/g, "<br />"); 

however, what I'm trying to achieve now converts [b] [/b] to <strong> </strong> . This is what I have tried so far, but it does not seem to work. I find regex is pretty hard to understand.

 bold = message.replace(/\[b].*\[\/b]/g, '<strong>'); 

Can anyone point me in the right direction?

+4
source share
2 answers

Try this one ...

 bold = message.replace(/\[b\](.*?)\[\/b\]/g, '<strong>$1</strong>'); 
+3
source

Try

 '[b]asdf[/b]'.replace(/\[(\/?)b\]/g, '<$1strong>') 
+3
source

All Articles