简体   繁体   中英

JavaScript remove double brackets (with inner text) from HTML block element

I want to remove substrings like [[any content here]] (double-bracked text with brackets) from HTML block element.

When I use this, it works:

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

But when I try to replace my needed substring:

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

, it not works. Why?

Full outer 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>

Since . does not include space characters \\s , \\t , \\n (see the snippet below),
you have to change your regex this way: /\\[\\[[\\s\\S]*?\\]\\]/g .
It means "zero or more 'space or/and not space' within double brackets".

 var p = document.getElementById('p').innerHTML; console.log(p.replace(/.*/, 'nothing')); console.log(p.replace(/(.|\\s)*/, 'nothing')); 
 <p id="p"> Several lines of text </p> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM