Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex remove line breaks

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...

like image 544
Gerald Ferreira Avatar asked Sep 10 '10 19:09

Gerald Ferreira


People also ask

How do I remove all line breaks from a string?

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.

Does trim remove line breaks?

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.


2 Answers

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, '');
like image 58
Aillyn Avatar answered Oct 13 '22 00:10

Aillyn


Try (.|\n|\r)*.

completehtml = completehtml.replace(/\<\!-- start-code-remove --\>(.|\n|\r)*?\<\!-- start-code-end --\>/ig, '');
like image 42
RightSaidFred Avatar answered Oct 12 '22 23:10

RightSaidFred