[英]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. 我想从HTML块元素中删除子字符串,例如
[[any content here]]
(带括号的双括号文本)。
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: 完整的外部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), 不包含空格字符
\\s
, \\t
, \\n
(请参见下面的代码段),
you have to change your regex this way: /\\[\\[[\\s\\S]*?\\]\\]/g
. 您必须通过以下方式更改您的正则表达式:
/\\[\\[[\\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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.