JavaScript: how to delete a line containing a specific line

How to delete a complete line if it contains a specific line, such as:

#RemoveMe
+5
source share
1 answer

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

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

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

Flag mhandles wildcard characters ^and $both the beginning and end of each line, rather than the beginning or end of the whole line.

+17
source

All Articles