简体   繁体   中英

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

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, '');

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

completehtml = completehtml.replace(/\<\!-- start-code-remove --\>(.|\n|\r)*?\<\!-- start-code-end --\>/ig, '');

Source

There is indeed no /s modifier to make the dot match all characters, including line breaks. To match absolutely any character, you can use character class that contains a shorthand class and its negated version, such as [\\s\\S].

Regex support in javascript is not very reliable.

function remove_tag_from_text(text, begin_tag, end_tag) {
    var tmp = text.split(begin_tag);
    while(tmp.length > 1) {
        var before = tmp.shift();
        var after = tmp.join(begin_tag).split(end_tag);
        after.shift();
        text = before + after.join(end_tag);
        tmp = text.split(begin_tag);
    }
    return text;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM