[removed] how to remove line that contain specific

2019-02-12 17:55发布

问题:

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

#RemoveMe

回答1:

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, "");

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.