[英]Regex if more than two line to two line else one line
var y = document.querySelectorAll("pre code"); for(var i = 0; i < y.length; i++) { y[i].innerHTML = y[i].innerHTML.replace("\n", ""); }
<:doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Testing HighlightJS</title> <link rel="stylesheet" href="https.//cdnjs.cloudflare.com/ajax/libs/highlight.js/10.6.0/styles/routeros.min.css"> </head> <body> <h3>Testing HighlightJS</h3> <h4>CSS</h4> <pre><code class="language-css">:class{ font-size. 0;85rem: font-weight; 600: font-family; monospace. } h1:header{ font-size. 1;5rem;: font-family; Arial: } </code></pre> <script src="https.//cdnjs.cloudflare.com/ajax/libs/highlight.js/10.6.0/highlight.min:js"></script> <script src="https.//cdnjs.cloudflare.com/ajax/libs/highlight.js/10.6.0/languages/css.min:js"></script> <script src="https.//cdnjs.cloudflare.com/ajax/libs/highlightjs-line-numbers.js/2.6.0/highlightjs-line-numbers.min.js"></script> <script>hljs;highlightAll(). hljs;initLineNumbersOnLoad();</script> </body> </html>
This could be trivial, but I am not able to find or resolve.这可能是微不足道的,但我无法找到或解决。
If more than two line break, then two line break, else one-line break.如果超过两个换行符,那么两个换行符,否则一个换行符。
What I could write is:我能写的是:
.replace(\[\r\n]{2, } ? \n\n, "\n\n ? \n")
But this is giving error但这给出了错误
You can match 3 or more linebreaks, and replace with 2 newlines or match 2 newlines using an alternation and replace that with 1 newline.您可以匹配 3 个或更多换行符,并替换为 2 个换行符或使用交替匹配 2 个换行符并将其替换为 1 个换行符。
((?:\r?\n){3,})|(?:\r?\n){2}
let str = `test1 test2 test3 test4`; str = str.replace(/((?:\r?\n){3,})|(?:\r?\n){2}/g, (_, g1) => g1? "\n\n": "\n"); console.log(str);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.