I am hoping that someone can help me. I am not exactly sure how to use the following regex. I am using classic ASP with Javascript
completehtml = completehtml.replace(/\<\!-- start-code-remove --\>.*?\<\!-- start-code-end --\>/ig, '');
I have this code to remove everything between
<\!-- start-code-remove --\>
and<\!-- start-code-end --\>
It works perfect up to the point where there is line breaks
in the values between start
and end
code...
How will I write the regex to remove everything between start
and end
even if there is line breaks
Thanks a million for responding...
Shoud I use the \n
and \s
characters not 100% sure..
(/\<\!-- start-code-remove --\>\s\n.*?\s\n\<\!-- start-code-end --\>/ig, '');
also the code should not be greedy between <\!-- start-code-remove --\> <\!-- start-code-end --\>/
and capture the values in groups...
There could be 3 or more of these sets...
Remove All Line Breaks from a String We can remove all line breaks by using a regex to match all the line breaks by writing: str = str. replace(/(\r\n|\n|\r)/gm, ""); \r\n is the CRLF line break used by Windows.
trim method removes any line breaks from the start and end of a string. It handles all line terminator characters (LF, CR, etc). The method also removes any leading or trailing spaces or tabs. The trim() method doesn't change the original string, it returns a new string.
The dot doesn't match new lines in Javascript, nor is there a modifier to make it do that (unlike in most modern regex engines). A common work-around is to use this character class in place of the dot: [\s\S]
. So your regex becomes:
completehtml = completehtml.replace(
/\<\!-- start-code-remove --\>[\s\S]*?\<\!-- start-code-end --\>/ig, '');
Try (.|\n|\r)*
.
completehtml = completehtml.replace(/\<\!-- start-code-remove --\>(.|\n|\r)*?\<\!-- start-code-end --\>/ig, '');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With