【问题标题】:Javascript regex, make remove single paragraph line breaksJavascript正则表达式,删除单段换行符
【发布时间】:2016-01-03 16:54:07
【问题描述】:

我有这种格式的文本:

word word,
word word.

word word
word word.

不特定于那两个单词的格式,它只是在这么多字符之前的换行符,而不是一长串的段落。但我试图让它成为一长串的段落。所以它应该是这样的:

word word, word word.
word word word word.

如果我使用代码 text.replace(/$\n(?=.)/gm, " ") 并将其输出到终端,我会得到如下所示的文本:

 word word, word word.
 word word word word.

它在段落的开头有一个额外的空间,但这对于我正在尝试做的事情来说已经足够了(尽管如果还有一种方法可以在一个替换函数中删除它,那就更好了)。问题是,当我将其输出到 textarea 时,它不会删除 \n 字符,而我只会得到如下所示的文本:

 word word,
 word word.

 word word
 word word.

我正在尝试在所有客户端执行此操作,目前在 Firefox 中运行它。

我不是最擅长正则表达式的,所以这可能真的很简单,我只是不知道如何去做。但任何帮助将不胜感激。谢谢!

【问题讨论】:

    标签: javascript regex


    【解决方案1】:

    回车是 \r 所以你需要使用

    text.replace(/$(\r|\n)(?=.)/gm, " ");

    【讨论】:

      【解决方案2】:

      在满足您要求的代码 sn-p 下方,我也删除了前导空格(由空行引起),使用带有替换功能的闭包:

      var regex  = /([^.])\s+/g;
      
      var input  = 'word word,\nword word.\n\nword word\nword word.';
      
      var result = input.replace(regex, function(all, char) {
        return (char.match(/\s/)) ? char : char + ' ' ;
      });
      
      document.write('<b>INPUT</b> <xmp>' + input + '</xmp>');
      document.write('<b>OUTPUT</b> <xmp>' + result + '</xmp>');

      正则表达式突破

      ([^.])        # Select any char that is not a literal dot '.'
                    # and save it in group $1
      \s+           # 1 or more whitespace char, remove trailing spaces (tabs too)
                    # and all type of newlines (\r\n, \r, \n)
      

      注意

      如果出于某种原因您想保留前导空格,请将以下代码简化如下:

      var regex   = /([^.])\s+/g;
      var replace = '$1 ';
      
      var input   = 'word word,\nword word.\n\nword word\nword word.';
      
      var result = input.replace(regex, replace);
      
      document.write('<b>INPUT</b> <xmp>' + input + '</xmp>');
      document.write('<b>OUTPUT</b> <xmp>' + result + '</xmp>');

      【讨论】:

        【解决方案3】:

        您可能错过了一些 \r,这是一种匹配所有类型的新行并且没有多余空格的方法:

        var input = 'word word,\nword word.\n\nword word\nword word.';
        
                    // split if 2 or more new lines
        var out = input.split(/(\r\n|\n|\r){2,}?/)
                    // split the paragraph by new lines and join the lines by a space
                    .map((v) => v.split(/\r\n|\n|\r/).join(' '))
                    // there is some spaces hanging in the array, filter them
                    .filter((v) => v.trim())
                    // join together all paragraphs by \n
                    .join('\n');
        
        $('#txt').append(out);
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        
        <textarea id="txt"></textarea>

        【讨论】:

        • 我认为您的意思是 (\r\n|\n|\r){2,} 而不是 [\r\n|\n|\r]{2,}
        • 试试这个:'this||is||a||test||'.split(/[\r\n|\n|\r]{2,}/)。输出:["this", "is", "a", "test", ""] 如果您想使用交替,请将方括号更改为圆括号,chars 类不能以这种方式工作。
        猜你喜欢
        • 2011-07-01
        • 2013-06-22
        • 1970-01-01
        • 1970-01-01
        • 2019-02-16
        • 2020-01-07
        • 1970-01-01
        • 1970-01-01
        • 2020-08-24
        相关资源
        最近更新 更多