user1888955 user1888955 - 26 days ago 7
Javascript Question

Can't remove place holders with regular expression in Javascript

I have a dynamically created table containing some redundant place holders that I would like to remove, they look like this:

{firm[i][j]} //i,j are numbers


I am trying regular expression in JavaScript but it doesn't work, here is my regular expression, table below is a string which will be inserted into DOM.

var table = "

<table class='table table-sm' style='margin:auto;'>
<thead>
<tr>
<th colspan='5'>QARELEASE</th>
</tr>
</thead>
<tbody>
<tr style='text-align:left;'>
<td width='25%;'>{firm[i][j]}</td>
<td width='25%;'>{firm[i][j]}</td>
<td width='25%;'>{firm[i][j]}</td>
<td width='25%;'>{firm[i][j]}</td>
</tr>
</tbody>

</table>"


regular expression:

table = table.replace(/{firm[\d{1}][\d{1}]}/g, "");


Not quite sure why it couldn't work

Answer

If you are trying to match [] too, you will need to escape them:

table = table.replace(/{firm\[\d{1}\]\[\d{1}\]}/g, "");