Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery replace square brackets

I'm trying to convert square brackets on a form submit. I won't go into the form submission part as it's not required.

I had no problem replacing a new line with a <br /> tag like so:

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

however what I'm trying to achieve now is convert [b] [/b] to <strong> </strong>. This is what I have tried so far but it doesn't seem to be working. I find regex's rather hard to grasp.

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

Can someone please point me in the right direction?

like image 548
Lodder Avatar asked Sep 23 '13 04:09

Lodder


People also ask

How do you remove square brackets from string?

Brackets can be removed from a string in Javascript by using a regular expression in combination with the . replace() method.

How do you change square brackets in Java?

String str = "[Chrissman-@1]"; str = replaceAll("\\[\\]", ""); String[] temp = str. split("-@"); System. out. println("Nickname: " + temp[0] + " | Power: " + temp[1]);

What does this regex do?

Short for regular expression, a regex is a string of text that lets you create patterns that help match, locate, and manage text. Perl is a great example of a programming language that utilizes regular expressions. However, its only one of the many places you can find regular expressions.


2 Answers

Try this one...

bold = message.replace(/\[b\](.*?)\[\/b\]/g, '<strong>$1</strong>');
like image 151
Thalaivar Avatar answered Sep 29 '22 04:09

Thalaivar


Try

'[b]asdf[/b]'.replace(/\[(\/?)b\]/g, '<$1strong>')
like image 32
Arun P Johny Avatar answered Sep 29 '22 05:09

Arun P Johny