JavaScript:如何删除包含特定字符串的行 [英] JavaScript: how to remove line that contain specific string

查看:63
本文介绍了JavaScript:如何删除包含特定字符串的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果包含如下所示的特定字符串,如何删除完整的行?

How can I remove a complete line if it contains a specific string like the following?

#RemoveMe


推荐答案

如果你有一个多行字符串,你可以使用一个RegExp m 标志:

If you have a multi-line string, you could use a RegExp with the m flag:

var str = 'line1\n'+
'line2\n'+
'#RemoveMe line3\n'+
'line4';

str.replace(/^.*#RemoveMe.*$/mg, "");

m 标志将处理 ^ $ 元字符作为每行的开头和结尾,而不是整个字符串的开头或结尾。

The m flag will treat the ^ and $ meta characters as the beginning and end of each line, not the beginning or end of the whole string.

这篇关于JavaScript:如何删除包含特定字符串的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆