Javascript Regular Expression Remove Spaces





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







70















So i'm writing a tiny little plugin for JQuery to remove spaces from a string. see here



(function($) {
$.stripSpaces = function(str) {
var reg = new RegExp("[ ]+","g");
return str.replace(reg,"");
}
})(jQuery);


my regular expression is currently [ ]+ to collect all spaces.
This works.. however It doesn't leave a good taste in my mouth..
I also tried [s]+ and [W]+ but neither worked..



There has to be a better (more concise) way of searching for only spaces.










share|improve this question





























    70















    So i'm writing a tiny little plugin for JQuery to remove spaces from a string. see here



    (function($) {
    $.stripSpaces = function(str) {
    var reg = new RegExp("[ ]+","g");
    return str.replace(reg,"");
    }
    })(jQuery);


    my regular expression is currently [ ]+ to collect all spaces.
    This works.. however It doesn't leave a good taste in my mouth..
    I also tried [s]+ and [W]+ but neither worked..



    There has to be a better (more concise) way of searching for only spaces.










    share|improve this question

























      70












      70








      70


      16






      So i'm writing a tiny little plugin for JQuery to remove spaces from a string. see here



      (function($) {
      $.stripSpaces = function(str) {
      var reg = new RegExp("[ ]+","g");
      return str.replace(reg,"");
      }
      })(jQuery);


      my regular expression is currently [ ]+ to collect all spaces.
      This works.. however It doesn't leave a good taste in my mouth..
      I also tried [s]+ and [W]+ but neither worked..



      There has to be a better (more concise) way of searching for only spaces.










      share|improve this question














      So i'm writing a tiny little plugin for JQuery to remove spaces from a string. see here



      (function($) {
      $.stripSpaces = function(str) {
      var reg = new RegExp("[ ]+","g");
      return str.replace(reg,"");
      }
      })(jQuery);


      my regular expression is currently [ ]+ to collect all spaces.
      This works.. however It doesn't leave a good taste in my mouth..
      I also tried [s]+ and [W]+ but neither worked..



      There has to be a better (more concise) way of searching for only spaces.







      javascript regex






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 22 '11 at 17:28









      rlemonrlemon

      13.8k1078116




      13.8k1078116
























          6 Answers
          6






          active

          oldest

          votes


















          172














          I would recommend you to use the literal notation, and using the s character class:



          //..
          return str.replace(/s/g, '');
          //..


          There's a difference between using the character class s and just ' ', this will match a lot of more white-space characters, for example 'trn' etc.., looking for ' ' will replace only the ASCII 32 blank space.



          The RegExp constructor is useful when you want to build a dynamic pattern, in this case you don't need it.



          Moreover, as you said, "[s]+" didn't worked with the RegExp constructor, that's because you are passing a string, and you should "double escape" the back-slashes, otherwise they will be interpreted as character escapes inside the string (e.g.: "s" === "s" (unknown escape)).






          share|improve this answer































            12














            "foo is bar".replace(/ /g, '')





            share|improve this answer































              1














              str.replace(/s/g,'')


              Works for me.



              jQuery.trim has the following hack for IE, although I'm not sure what versions it affects:



              // Check if a string has a non-whitespace character in it
              rnotwhite = /S/

              // IE doesn't match non-breaking spaces with s
              if ( rnotwhite.test( "xA0" ) ) {
              trimLeft = /^[sxA0]+/;
              trimRight = /[sxA0]+$/;
              }





              share|improve this answer































                0














                This works just as well: http://jsfiddle.net/maniator/ge59E/3/



                var reg = new RegExp(" ","g"); //<< just look for a space.





                share|improve this answer































                  0














                  In production



                  This is used in several apps to clean user-generated content removing extra spacing/returns etc but retains the meaning of spaces.



                  text.replace(/[nrst]+/g, ' ')





                  share|improve this answer































                    -1














                    Remove all spaces in string



                    // Remove only spaces
                    `
                    Text with spaces 1 1 1 1
                    and some
                    breaklines

                    `.replace(/ /g,'');
                    "
                    Textwithspaces1111
                    andsome
                    breaklines

                    "

                    // Remove spaces and breaklines
                    `
                    Text with spaces 1 1 1 1
                    and some
                    breaklines

                    `.replace(/s/g,'');
                    "Textwithspaces1111andsomebreaklines"





                    share|improve this answer
























                      protected by bummi Dec 11 '14 at 19:44



                      Thank you for your interest in this question.
                      Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                      Would you like to answer one of these unanswered questions instead?














                      6 Answers
                      6






                      active

                      oldest

                      votes








                      6 Answers
                      6






                      active

                      oldest

                      votes









                      active

                      oldest

                      votes






                      active

                      oldest

                      votes









                      172














                      I would recommend you to use the literal notation, and using the s character class:



                      //..
                      return str.replace(/s/g, '');
                      //..


                      There's a difference between using the character class s and just ' ', this will match a lot of more white-space characters, for example 'trn' etc.., looking for ' ' will replace only the ASCII 32 blank space.



                      The RegExp constructor is useful when you want to build a dynamic pattern, in this case you don't need it.



                      Moreover, as you said, "[s]+" didn't worked with the RegExp constructor, that's because you are passing a string, and you should "double escape" the back-slashes, otherwise they will be interpreted as character escapes inside the string (e.g.: "s" === "s" (unknown escape)).






                      share|improve this answer




























                        172














                        I would recommend you to use the literal notation, and using the s character class:



                        //..
                        return str.replace(/s/g, '');
                        //..


                        There's a difference between using the character class s and just ' ', this will match a lot of more white-space characters, for example 'trn' etc.., looking for ' ' will replace only the ASCII 32 blank space.



                        The RegExp constructor is useful when you want to build a dynamic pattern, in this case you don't need it.



                        Moreover, as you said, "[s]+" didn't worked with the RegExp constructor, that's because you are passing a string, and you should "double escape" the back-slashes, otherwise they will be interpreted as character escapes inside the string (e.g.: "s" === "s" (unknown escape)).






                        share|improve this answer


























                          172












                          172








                          172







                          I would recommend you to use the literal notation, and using the s character class:



                          //..
                          return str.replace(/s/g, '');
                          //..


                          There's a difference between using the character class s and just ' ', this will match a lot of more white-space characters, for example 'trn' etc.., looking for ' ' will replace only the ASCII 32 blank space.



                          The RegExp constructor is useful when you want to build a dynamic pattern, in this case you don't need it.



                          Moreover, as you said, "[s]+" didn't worked with the RegExp constructor, that's because you are passing a string, and you should "double escape" the back-slashes, otherwise they will be interpreted as character escapes inside the string (e.g.: "s" === "s" (unknown escape)).






                          share|improve this answer













                          I would recommend you to use the literal notation, and using the s character class:



                          //..
                          return str.replace(/s/g, '');
                          //..


                          There's a difference between using the character class s and just ' ', this will match a lot of more white-space characters, for example 'trn' etc.., looking for ' ' will replace only the ASCII 32 blank space.



                          The RegExp constructor is useful when you want to build a dynamic pattern, in this case you don't need it.



                          Moreover, as you said, "[s]+" didn't worked with the RegExp constructor, that's because you are passing a string, and you should "double escape" the back-slashes, otherwise they will be interpreted as character escapes inside the string (e.g.: "s" === "s" (unknown escape)).







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Aug 22 '11 at 17:34









                          CMSCMS

                          603k163848815




                          603k163848815

























                              12














                              "foo is bar".replace(/ /g, '')





                              share|improve this answer




























                                12














                                "foo is bar".replace(/ /g, '')





                                share|improve this answer


























                                  12












                                  12








                                  12







                                  "foo is bar".replace(/ /g, '')





                                  share|improve this answer













                                  "foo is bar".replace(/ /g, '')






                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Aug 22 '11 at 17:31









                                  Ransom BriggsRansom Briggs

                                  1,71732643




                                  1,71732643























                                      1














                                      str.replace(/s/g,'')


                                      Works for me.



                                      jQuery.trim has the following hack for IE, although I'm not sure what versions it affects:



                                      // Check if a string has a non-whitespace character in it
                                      rnotwhite = /S/

                                      // IE doesn't match non-breaking spaces with s
                                      if ( rnotwhite.test( "xA0" ) ) {
                                      trimLeft = /^[sxA0]+/;
                                      trimRight = /[sxA0]+$/;
                                      }





                                      share|improve this answer




























                                        1














                                        str.replace(/s/g,'')


                                        Works for me.



                                        jQuery.trim has the following hack for IE, although I'm not sure what versions it affects:



                                        // Check if a string has a non-whitespace character in it
                                        rnotwhite = /S/

                                        // IE doesn't match non-breaking spaces with s
                                        if ( rnotwhite.test( "xA0" ) ) {
                                        trimLeft = /^[sxA0]+/;
                                        trimRight = /[sxA0]+$/;
                                        }





                                        share|improve this answer


























                                          1












                                          1








                                          1







                                          str.replace(/s/g,'')


                                          Works for me.



                                          jQuery.trim has the following hack for IE, although I'm not sure what versions it affects:



                                          // Check if a string has a non-whitespace character in it
                                          rnotwhite = /S/

                                          // IE doesn't match non-breaking spaces with s
                                          if ( rnotwhite.test( "xA0" ) ) {
                                          trimLeft = /^[sxA0]+/;
                                          trimRight = /[sxA0]+$/;
                                          }





                                          share|improve this answer













                                          str.replace(/s/g,'')


                                          Works for me.



                                          jQuery.trim has the following hack for IE, although I'm not sure what versions it affects:



                                          // Check if a string has a non-whitespace character in it
                                          rnotwhite = /S/

                                          // IE doesn't match non-breaking spaces with s
                                          if ( rnotwhite.test( "xA0" ) ) {
                                          trimLeft = /^[sxA0]+/;
                                          trimRight = /[sxA0]+$/;
                                          }






                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Aug 22 '11 at 17:31









                                          Stefan KendallStefan Kendall

                                          41.7k56213374




                                          41.7k56213374























                                              0














                                              This works just as well: http://jsfiddle.net/maniator/ge59E/3/



                                              var reg = new RegExp(" ","g"); //<< just look for a space.





                                              share|improve this answer




























                                                0














                                                This works just as well: http://jsfiddle.net/maniator/ge59E/3/



                                                var reg = new RegExp(" ","g"); //<< just look for a space.





                                                share|improve this answer


























                                                  0












                                                  0








                                                  0







                                                  This works just as well: http://jsfiddle.net/maniator/ge59E/3/



                                                  var reg = new RegExp(" ","g"); //<< just look for a space.





                                                  share|improve this answer













                                                  This works just as well: http://jsfiddle.net/maniator/ge59E/3/



                                                  var reg = new RegExp(" ","g"); //<< just look for a space.






                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Aug 22 '11 at 17:29









                                                  NealNeal

                                                  114k31214274




                                                  114k31214274























                                                      0














                                                      In production



                                                      This is used in several apps to clean user-generated content removing extra spacing/returns etc but retains the meaning of spaces.



                                                      text.replace(/[nrst]+/g, ' ')





                                                      share|improve this answer




























                                                        0














                                                        In production



                                                        This is used in several apps to clean user-generated content removing extra spacing/returns etc but retains the meaning of spaces.



                                                        text.replace(/[nrst]+/g, ' ')





                                                        share|improve this answer


























                                                          0












                                                          0








                                                          0







                                                          In production



                                                          This is used in several apps to clean user-generated content removing extra spacing/returns etc but retains the meaning of spaces.



                                                          text.replace(/[nrst]+/g, ' ')





                                                          share|improve this answer













                                                          In production



                                                          This is used in several apps to clean user-generated content removing extra spacing/returns etc but retains the meaning of spaces.



                                                          text.replace(/[nrst]+/g, ' ')






                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered Jan 13 at 16:28









                                                          Jason SebringJason Sebring

                                                          13.4k66264




                                                          13.4k66264























                                                              -1














                                                              Remove all spaces in string



                                                              // Remove only spaces
                                                              `
                                                              Text with spaces 1 1 1 1
                                                              and some
                                                              breaklines

                                                              `.replace(/ /g,'');
                                                              "
                                                              Textwithspaces1111
                                                              andsome
                                                              breaklines

                                                              "

                                                              // Remove spaces and breaklines
                                                              `
                                                              Text with spaces 1 1 1 1
                                                              and some
                                                              breaklines

                                                              `.replace(/s/g,'');
                                                              "Textwithspaces1111andsomebreaklines"





                                                              share|improve this answer






























                                                                -1














                                                                Remove all spaces in string



                                                                // Remove only spaces
                                                                `
                                                                Text with spaces 1 1 1 1
                                                                and some
                                                                breaklines

                                                                `.replace(/ /g,'');
                                                                "
                                                                Textwithspaces1111
                                                                andsome
                                                                breaklines

                                                                "

                                                                // Remove spaces and breaklines
                                                                `
                                                                Text with spaces 1 1 1 1
                                                                and some
                                                                breaklines

                                                                `.replace(/s/g,'');
                                                                "Textwithspaces1111andsomebreaklines"





                                                                share|improve this answer




























                                                                  -1












                                                                  -1








                                                                  -1







                                                                  Remove all spaces in string



                                                                  // Remove only spaces
                                                                  `
                                                                  Text with spaces 1 1 1 1
                                                                  and some
                                                                  breaklines

                                                                  `.replace(/ /g,'');
                                                                  "
                                                                  Textwithspaces1111
                                                                  andsome
                                                                  breaklines

                                                                  "

                                                                  // Remove spaces and breaklines
                                                                  `
                                                                  Text with spaces 1 1 1 1
                                                                  and some
                                                                  breaklines

                                                                  `.replace(/s/g,'');
                                                                  "Textwithspaces1111andsomebreaklines"





                                                                  share|improve this answer















                                                                  Remove all spaces in string



                                                                  // Remove only spaces
                                                                  `
                                                                  Text with spaces 1 1 1 1
                                                                  and some
                                                                  breaklines

                                                                  `.replace(/ /g,'');
                                                                  "
                                                                  Textwithspaces1111
                                                                  andsome
                                                                  breaklines

                                                                  "

                                                                  // Remove spaces and breaklines
                                                                  `
                                                                  Text with spaces 1 1 1 1
                                                                  and some
                                                                  breaklines

                                                                  `.replace(/s/g,'');
                                                                  "Textwithspaces1111andsomebreaklines"






                                                                  share|improve this answer














                                                                  share|improve this answer



                                                                  share|improve this answer








                                                                  edited Jan 3 at 22:21

























                                                                  answered Dec 12 '18 at 20:57









                                                                  DarckBlezzerDarckBlezzer

                                                                  2,04612130




                                                                  2,04612130

















                                                                      protected by bummi Dec 11 '14 at 19:44



                                                                      Thank you for your interest in this question.
                                                                      Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                                                      Would you like to answer one of these unanswered questions instead?



                                                                      Popular posts from this blog

                                                                      Mossoró

                                                                      Error while reading .h5 file using the rhdf5 package in R

                                                                      Pushsharp Apns notification error: 'InvalidToken'