[英]Find and replace HTML string with jQuery
我建议:
$('#relatedinfo ul li a').html(function(index,html){
return html.replace(/<\/span>(\:)/,'');
});
JS小提琴演示 。
甚至:
$('#relatedinfo ul li a').text(function(index,text){
return text.replace(':','');
});
JS小提琴演示 。
更新的方法是检查的最后一个字符span
是阵列中['?','!','.']
如果是的话,删除:
从nextSibling的的nodeValue:
$('#relatedinfo ul li a span').text(function(index,text){
var lastchar = text.split('').pop();
if (['?','!','.'].indexOf(lastchar) > -1) {
this.nextSibling.nodeValue = this.nextSibling.nodeValue.replace(':','');
}
});
JS小提琴演示 。
参考文献:
html()
。 String.replace()
。 text()
。 你也可以使用正则表达式:
var rx = new RegExp('(?![^<&]+[>;])' + searchString, 'gi');
$('#relatedinfo').html(function (i, html) {
return html.replace(rx, '<b>$&</b>')
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.