Reputation: 165
I have found a short script which finds a line with a certain phrase in it and replaces the whole line with a new line. However I can't work out how to include a variable where it says '#RemoveMe' in the str.replace. (I want to include the variable 'start' which is passed in by the function's trigger, to make --> (/^.start.$/mg, ""))
Any help appreciated...
var str = 'line1\n'+
'line2\n'+
'#RemoveMe line3\n'+
'line4';
var test = str.replace(/^.*#RemoveMe.*$/mg, "");
Thanks.
Upvotes: 2
Views: 174
Reputation: 57709
This is a limitation of the inline regular expression notation.
If you use new RegExp()
you can pass the expression as a string.
var find = "start";
var regexp = new RegExp("^.*" + find + ".*$", "mg"); // note: no delimiters
str.replace(regexp, "");
Upvotes: 6