JavaScript从HTML块元素中删除双括号(带有内部文本)

时间:2018-02-08 14:47:42

标签: javascript jquery html replace modx

我想从HTML块元素中删除[[any content here]](带括号的双括号文本)等子串。

当我使用它时,它可以工作:

$('#comment-263353 .ticket-comment-body').html('Replaced with this text successfully!');

但是当我尝试替换我需要的子字符串时:

$('#comment-263353 .ticket-comment-body').html($('#'+com_id+' .ticket-comment-body').html().replace(/\[\[.*?\]\]/g, ''));

,它不起作用。为什么呢?

全外部HTML:

<li class="ticket-comment" id="comment-263353" data-parent="263338" data-newparent="263338" data-id="263353" xmlns:v="http://rdf.data-vocabulary.org/#" typeof="v:Review">
    <!--ID: 263353-->
    <span class="hidden" property="v:itemreviewed"></span>
    <div class="ticket-comment-body">
        <div class="ticket-comment-header">
            <div class="ticket-comment-dot-wrapper"><div class="ticket-comment-dot"></div></div>
            <span class="ticket-comment-author" property="v:reviewer">admin</span>
            <span class="ticket-comment-createdon" property="v:dtreviewed" content="">Just now</span>
            <span class="ticket-comment-star active"><i class="glyphicon glyphicon-star unstared star"></i></span>
            <span class="ticket-comment-up"><a href="http://beta.mirparfuma.com/vopros-otvet#comment-263338" data-id="263353" data-parent="263338">↑</a></span>
            <span class="ticket-comment-down"><a href="#" data-child="">↓</a></span>
        </div>
        <div class="ticket-comment-text" property="v:description">
            Any text
        </div>
[[*id:isnot='47304':then='
<p class="rating">Rate:
<span property="v:rating" class="star hidden">5</span>
<span class="star icon_star star_active"></span><span class="star icon_star star_active"></span><span class="star icon_star star_active"></span><span class="star icon_star star_active"></span><span class="star icon_star star_active"></span>
</p>
']]
    </div>
    <div class="comment-reply">
        <a href="#" class="reply">Reply</a>
        <a href="#" class="edit btn btn-default btn-xs">Edit</a>
    </div>
    <ol class="comments-list"></ol>
</li>

1 个答案:

答案 0 :(得分:2)

由于<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>JS Bin</title> </head> <body> <div id="tableContainer"></div> </body> </html> 不包含空格字符.\s\t(请参阅下面的代码段), 你必须以这种方式改变你的正则表达式:\n
它意味着零个或多个空间或/和空间&#39;在双括号内#34;。

&#13;
&#13;
/\[\[[\s\S]*?\]\]/g
&#13;
var p = document.getElementById('p').innerHTML;

console.log(p.replace(/.*/, 'nothing'));
console.log(p.replace(/(.|\s)*/, 'nothing'));
&#13;
&#13;
&#13;