Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace text inside of square brackets

var a = "[i] earned [c] coin for [b] bonus";

How to get string "__ earned __ coin for __ bonus" from the variable above in JavaScript?

All I want to do is to replace all the bracket [] and its content to __.

like image 997
theHack Avatar asked May 24 '11 09:05

theHack


1 Answers

a = a.replace(/\[.*?\]/g, '__');

if you expect newlines to be possible, you can use:

a = a.replace(/\[[^\]]*?\]/g, '__');
like image 97
Brett Zamir Avatar answered Sep 21 '22 00:09

Brett Zamir