jquery replace with wild card


at present i am using this to replace single replacement but i want to replace all stings like [timer1] [timer2] etc

  var final_before_message = message_before.replace("[timer]", "<b id="timer"></b>");

it works fine but it will work only for [timer] i want to replace any occurences of words like [timer1] timer2] [timer3] upto [timer9] with etc

for example

       var final_before_message = message_before.replace("[timer1]", "<b id="timer1"></b>");

      var final_before_message = message_before.replace("[timer2]", "<b id="timer2"></b>");

     var final_before_message = message_before.replace("[timer3]", "<b id="timer3"></b>");

      var final_before_message = message_before.replace("[timer9]", "<b id="timer9"></b>");

can i use some wildcard in replace and achieve this ?/ with one line code ??


Use a regular expression replacement with a capture group.

var final_before_message = message_before.replace(/[timer(d+)]/g, '<b id="timer$1"></b>');

$1 is replaced by the number that was matched by the capture group (d+).

Source: stackoverflow