Limit more than one linebreak, limit double space, RegEx replace

91 Views Asked by At

I'm having trouble limiting a string to prevent multiple spaces and multiple linebreaks. This reformat is gonna happen to an input value before uploading to server.

Ex:

Input:

    i am    a   text
with

little



to no


meaning    asdas

Expected output:

i am a text
with

little

to no

meaning asdas

I googled this but it only helps with linebreaks.

str.replace(/\n\s*\n/g, '\n\n')
3

There are 3 best solutions below

1
The fourth bird On BEST ANSWER

Using \s can also match a newline. Instead you can use a negated character class to match a whitespace character without a newline [^\S\n]

You could:

  • Trim the string

  • Match 2 or more newlines followed by whitespace chars without newlines using ^(?:\n[^\S\n]*){2,} and replace with a single newline

  • Match 2 or more whitespace chars without a newline [^\S\n]{2,} and replace with a single space.

const s = `    i am    a   text
with

little



to no


meaning    asdas`;
const result = s.trim()
  .replace(/^(?:\n[^\S\n]*){2,}/gm, "\n")
  .replace(/[^\S\n]{2,}/g, " ");

console.log(result);

3
AKX On

First replace more than 2 newlines with exactly 2 newlines, then replace multiple spaces with a single space?

var t = `
i am    a   text
with

little



to no


meaning    asdas
`.trim();
console.log(t.replace(/\n{2,}/g, '\n\n').replace(/[ ]{2,}/g, ' '));

0
Andrei Odegov On

The second parameter of String.prototype.replace() can be a function.

const regex = /((?<![\S\s])\s+)|(\s+(?![\S\s]))|((?:[^\S\n]*\n){2,})|(^\s+)|(\s+$)|(\s{2,})/gm;

const str = `    i am    a   text   
with

little
    

   

     foo   
to no  


meaning    asdas  
   
  `;

const result = str.replace(regex, (_, ...args) => ['', '', '\n\n', '', '', ' '][args.findIndex(it => it !== undefined)]);

console.log(`Substitution result:\n"${result}"`);

The regex used in the snippet can be broken down as follows.

 (              group and capture to \1:
-----------------------------------------------------------
   (?<!           look behind to see if there is not:
-----------------------------------------------------------
     [\S\s]         any character of: non-whitespace (all
                    but \n, \r, \t, \f, and " "),
                    whitespace (\n, \r, \t, \f, and " ")
-----------------------------------------------------------
   )              end of look-behind
-----------------------------------------------------------
   \s+            whitespace (\n, \r, \t, \f, and " ") (1
                  or more times (matching the most amount
                  possible))
-----------------------------------------------------------
 )              end of \1
-----------------------------------------------------------
|              OR
-----------------------------------------------------------
 (              group and capture to \2:
-----------------------------------------------------------
   \s+            whitespace (\n, \r, \t, \f, and " ") (1
                  or more times (matching the most amount
                  possible))
-----------------------------------------------------------
   (?!            look ahead to see if there is not:
-----------------------------------------------------------
     [\S\s]         any character of: non-whitespace (all
                    but \n, \r, \t, \f, and " "),
                    whitespace (\n, \r, \t, \f, and " ")
-----------------------------------------------------------
   )              end of look-ahead
-----------------------------------------------------------
 )              end of \2
-----------------------------------------------------------
|              OR
-----------------------------------------------------------
 (              group and capture to \3:
-----------------------------------------------------------
   (?:            group, but do not capture (at least 2
                  times (matching the most amount
                  possible)):
-----------------------------------------------------------
     [^\S\n]*       any character except: non-whitespace
                    (all but \n, \r, \t, \f, and " "),
                    '\n' (newline) (0 or more times
                    (matching the most amount possible))
-----------------------------------------------------------
     \n             '\n' (newline)
-----------------------------------------------------------
   ){2,}          end of grouping
-----------------------------------------------------------
 )              end of \3
-----------------------------------------------------------
|              OR
-----------------------------------------------------------
 (              group and capture to \4:
-----------------------------------------------------------
   ^              the beginning of the string
-----------------------------------------------------------
   \s+            whitespace (\n, \r, \t, \f, and " ") (1
                  or more times (matching the most amount
                  possible))
-----------------------------------------------------------
 )              end of \4
-----------------------------------------------------------
|              OR
-----------------------------------------------------------
 (              group and capture to \5:
-----------------------------------------------------------
   \s+            whitespace (\n, \r, \t, \f, and " ") (1
                  or more times (matching the most amount
                  possible))
-----------------------------------------------------------
   $              before an optional \n, and the end of
                  the string
-----------------------------------------------------------
 )              end of \5
-----------------------------------------------------------
|              OR
-----------------------------------------------------------
 (              group and capture to \6:
-----------------------------------------------------------
   \s{2,}         whitespace (\n, \r, \t, \f, and " ") (at
                  least 2 times (matching the most amount
                  possible))
-----------------------------------------------------------
 )              end of \6