【问题标题】:Remove new lines within and around a specific text blok删除特定文本块内和周围的新行
【发布时间】:2022-10-14 00:52:28
【问题描述】:

我有一个包含类似文本的文本文件(我将\n 作为新行的指示符,因此它不存在于文本中):

\n
---
\n
\n
not fixed
number of 
lines
\n
\n
---

Other text
More text
...

三行之间的文字不是固定的,那里可能有很多行文字。 我想删除三行内和顶部的所有新行:

---
not fixed
number of 
lines
---

Other text
More text
...

目前我找到了如何删除第一行,但我正在努力思考如何省略其余部分:

myText = myText.split('\n').slice(1).join('\n'));

【问题讨论】:

    标签: javascript node.js


    【解决方案1】:

    使用myText.replace(/^ /gm, "")

    示例/演示:

    let text = `
    ---
    
    
    not fixed
    number of 
    lines
    
    
    ---
    
    Other text
    More text`;
    
    console.log(text.replace(/^
    /gm, ""));

    【讨论】:

      【解决方案2】:

      这样的事情应该做你:

      const s1 = "...";
      const s2 = s1.replace( /
      +---
      +/g , '
      ---
      ' ) ;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-01
        • 1970-01-01
        • 2020-08-18
        • 2022-11-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多