0

How can i merge the following into one regex?

    str
        .replace(/(\r\n|\n|\r)/gm,"") // get rid of line breaks 
        .replace(/\s/g, ""); // get rid of spaces

I want to call replace() once and take care of both in same regex

shmnsw
  • 639
  • 2
  • 11
  • 24

3 Answers3

3

I guess you only need to be using \s... that normally includes [\r\n\t\f ], so the first replace is basically useless.

https://regex101.com/r/eX7sE7/1

F.P
  • 17,421
  • 34
  • 123
  • 189
0

regexr.com is a great tool for testing regular expressions as it explains what each part does. for example if you hover over a /s it will tell you that it matches line breaks as well.

Gabor Magyar
  • 926
  • 2
  • 9
  • 20
0

use this str.replace(/(\s)/g,"");

Divesh Oswal
  • 250
  • 3
  • 13