Javascript 如何删除不包含特定字符串的行
您好,我正在尝试删除不包含特定字符串的行 假设我有台词Javascript 如何删除不包含特定字符串的行,javascript,php,jquery,Javascript,Php,Jquery,您好,我正在尝试删除不包含特定字符串的行 假设我有台词 <p>hello are news watch</p> <p>news watch</p> <p>news watch hour</p> <p>hey how</p> <p>hey news</p> 使用拆分字符串,然后使用和方法筛选出包含文本的行,最后使用方法将它们连接起来 var str=`hello are ne
<p>hello are news watch</p>
<p>news watch</p>
<p>news watch hour</p>
<p>hey how</p>
<p>hey news</p>
使用拆分字符串,然后使用和方法筛选出包含文本的行,最后使用方法将它们连接起来
var str=`hello are news watch
新闻观察
新闻观察时间
嘿,怎么了
嘿,新闻`;
console.log(
str.split(“\n”)
.过滤器(功能(v){
返回v.indexOf('news watch')>-1;
})
.join(“\n”)
)
您可以使用前面文章中提到的过滤器,也可以使用正则表达式。要在php中实现这一点,您可以将其拆分为一个行数组,然后使用preg\u grep。大多数高级语言都支持正则表达式
$(“p”)。每个(函数(){
var str=$(this.text();
如果(str.indexOf(“新闻观察”)分享你的尝试far@PranavCBalan如何在中不区分大小写this@babukumar:同时将两者转换为小写comparing@babukumarreturn$(this).text().toLowerCase().indexOf('newsweach')==-1;
hello are news watch
news watch
news watch hour
$("p").each(function(){
var str=$(this).text();
if (str.indexOf("news watch") <= 0)
{
$(this).hide(); // or use remove();
}
});