Reputation: 16157
Text To Remove:
I am trying to remove a line of text from a file.
Everything between <%'/testStart'%>
and <%'/testEnd'%
>` including the delimiters.
<%'/testStart'%> Some text with other random characters in between <%'/testEnd'%>
JavaScript:
I have tried this with no luck. Well, at one point I had it working with everything hard coded in the RegExp. But I have tried so many ways I can't remember what I did. Basically I think I am just not escaping something properly.
var p = "/test"; //this is dynamic
var start = "<%'" + p + "Start" + "'%>";
var end = "<%'" + p + "End" + "'%>";
var regex = new RegExp("\\" + start + "[^:]\\" + end);
var newData = data.replace(regex,"");
Expected Result:
Completely remove this line.
<%'/testStart'%> Some text with other random characters in between <%'/testEnd'%>
Any help is much appreciated. Thanks,
Upvotes: 2
Views: 578
Reputation: 41838
Match Inside with a Lazy .*?
Replace your inner section "[^:]\\"
with ".*?"
var regex = new RegExp( start + ".*?" + end );
The effect is to match everything up to the end parameter.
Explanation
The star quantifier in .*?
is made "lazy" by the ?
so that the dot only matches as many characters as needed to allow the next token to match (shortest match). Without the ?
, the .*
first matches the whole string, then backtracks only as far as needed to allow the next token to match (longest match).
Upvotes: 3