鉴于以下字符串,我尝试使用正则表达式匹配{
和}
之间的任何字符串,并用<tr style="display:none;"><td></td></tr>
包围它。
我尝试编写以下脚本但不起作用。
$(document).ready(function() {
table = ` <table border="1">
{% Acount Summary 2018 %}
<tbody>
{I}
<tr style="height: 25px;">
<td style="padding-left: 10px;">Account Number: </td>
{# II #}
<td>Account</td>
{% Active Account %}
</tr>
{ Requested Account #}
<tr style="height: 25px">
<td style="padding-left: 10px;">Number of Requests: </td>
<td>Request</td>
{{ III }}
</tr>
{#
<tr style="height: 25px">
<td style="padding-left: 10px;">Customer: </td>
<td>Contract Customer</td>
</tr>
#}
<tr style="height: 25px">
<td style="padding-left: 10px;"> Customoer ID</td>
<td>ID</td>
</tr>
{# CustomerName #}
</tbody>
{# Account #}
{{ Inactive }}
</table>
`;
table.replace(/({\s*?.*?.*})/g, '<tr style="display:none;"><td>$1</td></tr>');
console.log(table);
$('#COA').html(table);
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="COA"></div>
&#13;
缺少什么?我怎样才能纠正上面的脚本以达到上述效果?感谢。
答案 0 :(得分:2)
您需要重新分配变量table
:
table = table.replace( ... )
我修改了您的RegEx,使其更简单,并排除了{}
:{([^}]*)}
请注意,您的方法不会处理嵌套的{}
。你必须走它的递归之路。
$(document).ready(function() {
table = ` <table border="1">
{% Acount Summary 2018 %}
<tbody>
{I}
<tr style="height: 25px;">
<td style="padding-left: 10px;">Account Number: </td>
{# II #}
<td>Account</td>
{% Active Account %}
</tr>
{ Requested Account #}
<tr style="height: 25px">
<td style="padding-left: 10px;">Number of Requests: </td>
<td>Request</td>
{{ III }}
</tr>
{#
<tr style="height: 25px">
<td style="padding-left: 10px;">Customer: </td>
<td>Contract Customer</td>
</tr>
#}
<tr style="height: 25px">
<td style="padding-left: 10px;"> Customoer ID</td>
<td>ID</td>
</tr>
{# CustomerName #}
</tbody>
{# Account #}
{{ Inactive }}
</table>
`;
table = table.replace(/{([^}]*)}/g, '<tr style="display:none;"><td>$1</td></tr>');
console.log(table);
$('#COA').html(table);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="COA"></div>