Remove special symbols and extra spaces and replace with underscore using the replace method





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







29















I want to remove all special characters and spaces from a string and replace with an underscore.
The string is



    var str = "hello world & hello universe";


I have this now which replaces only spaces:



      str.replace(/s/g, "_");


The result I get is hello_world_&_hello_universe, but I would like to remove the special symbols as well.



I tried this str.replace(/[^a-zA-Z0-9]s/g, "_") but this does not help.










share|improve this question































    29















    I want to remove all special characters and spaces from a string and replace with an underscore.
    The string is



        var str = "hello world & hello universe";


    I have this now which replaces only spaces:



          str.replace(/s/g, "_");


    The result I get is hello_world_&_hello_universe, but I would like to remove the special symbols as well.



    I tried this str.replace(/[^a-zA-Z0-9]s/g, "_") but this does not help.










    share|improve this question



























      29












      29








      29


      11






      I want to remove all special characters and spaces from a string and replace with an underscore.
      The string is



          var str = "hello world & hello universe";


      I have this now which replaces only spaces:



            str.replace(/s/g, "_");


      The result I get is hello_world_&_hello_universe, but I would like to remove the special symbols as well.



      I tried this str.replace(/[^a-zA-Z0-9]s/g, "_") but this does not help.










      share|improve this question
















      I want to remove all special characters and spaces from a string and replace with an underscore.
      The string is



          var str = "hello world & hello universe";


      I have this now which replaces only spaces:



            str.replace(/s/g, "_");


      The result I get is hello_world_&_hello_universe, but I would like to remove the special symbols as well.



      I tried this str.replace(/[^a-zA-Z0-9]s/g, "_") but this does not help.







      javascript






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 11 '18 at 18:09









      Peter Mortensen

      14k1987114




      14k1987114










      asked Oct 22 '12 at 21:34









      Do GoodDo Good

      4173916




      4173916
























          4 Answers
          4






          active

          oldest

          votes


















          76














          Your regular expression [^a-zA-Z0-9]s/g says match any character that is not a number or letter followed by a space.



          Remove the s and you should get what you are after if you want a _ for every special character.



          var newString = str.replace(/[^A-Z0-9]/ig, "_");


          That will result in hello_world___hello_universe



          If you want it to be single underscores use a + to match multiple



          var newString = str.replace(/[^A-Z0-9]+/ig, "_");


          That will result in hello_world_hello_universe






          share|improve this answer































            3














            Remove the s from your new regex and it should work - whitespace is already included in "anything but alphanumerics".



            Note that you may want to add a + after the ] so you don't get sequences of more than one underscore. You can also chain onto .replace(/^_+|_+$/g,'') to trim off underscores at the start or end of the string.






            share|improve this answer
























            • Why call separate replace when you can add + in first regex?

              – MBO
              Oct 22 '12 at 21:38











            • Because... good point. I was thinking of something else, I guess. Still, you could chain on a trim-like regex.

              – Niet the Dark Absol
              Oct 22 '12 at 21:39



















            1














            It was not asked precisely to remove accent (only special characters), but I needed to.



            The solutions givens here works but they don’t remove accent: é, è, etc.



            So, before doing epascarello’s solution, you can also do:






            var newString = "développeur & intégrateur";

            newString = replaceAccents(newString);
            newString = newString.replace(/[^A-Z0-9]+/ig, "_");
            alert(newString);

            /**
            * Replaces all accented chars with regular ones
            */
            function replaceAccents(str) {
            // Verifies if the String has accents and replace them
            if (str.search(/[xC0-xFF]/g) > -1) {
            str = str
            .replace(/[xC0-xC5]/g, "A")
            .replace(/[xC6]/g, "AE")
            .replace(/[xC7]/g, "C")
            .replace(/[xC8-xCB]/g, "E")
            .replace(/[xCC-xCF]/g, "I")
            .replace(/[xD0]/g, "D")
            .replace(/[xD1]/g, "N")
            .replace(/[xD2-xD6xD8]/g, "O")
            .replace(/[xD9-xDC]/g, "U")
            .replace(/[xDD]/g, "Y")
            .replace(/[xDE]/g, "P")
            .replace(/[xE0-xE5]/g, "a")
            .replace(/[xE6]/g, "ae")
            .replace(/[xE7]/g, "c")
            .replace(/[xE8-xEB]/g, "e")
            .replace(/[xEC-xEF]/g, "i")
            .replace(/[xF1]/g, "n")
            .replace(/[xF2-xF6xF8]/g, "o")
            .replace(/[xF9-xFC]/g, "u")
            .replace(/[xFE]/g, "p")
            .replace(/[xFDxFF]/g, "y");
            }

            return str;
            }





            Source: https://gist.github.com/jonlabelle/5375315






            share|improve this answer

































              0














              If you have a text as



              var sampleText ="ä_öü_ßÄ_ TESTED Ö_Ü!@#$%^&())(&&++===.XYZ"



              To replace all special character (!@#$%^&())(&&++= ==.) without replacing the characters(including umlaut)



              Use below regex



              sampleText = sampleText.replace(/[`~!@#$%^&*()|+-=?;:'",.<>{}/s]/gi,'');



              OUTPUT : sampleText = "ä_öü_ßÄ____TESTED_Ö_Ü_____________________XYZ"



              This would replace all with an underscore which is provided as second argument to the replace function.You can add whatever you want as per your requirement






              share|improve this answer
























                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f13020246%2fremove-special-symbols-and-extra-spaces-and-replace-with-underscore-using-the-re%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                76














                Your regular expression [^a-zA-Z0-9]s/g says match any character that is not a number or letter followed by a space.



                Remove the s and you should get what you are after if you want a _ for every special character.



                var newString = str.replace(/[^A-Z0-9]/ig, "_");


                That will result in hello_world___hello_universe



                If you want it to be single underscores use a + to match multiple



                var newString = str.replace(/[^A-Z0-9]+/ig, "_");


                That will result in hello_world_hello_universe






                share|improve this answer




























                  76














                  Your regular expression [^a-zA-Z0-9]s/g says match any character that is not a number or letter followed by a space.



                  Remove the s and you should get what you are after if you want a _ for every special character.



                  var newString = str.replace(/[^A-Z0-9]/ig, "_");


                  That will result in hello_world___hello_universe



                  If you want it to be single underscores use a + to match multiple



                  var newString = str.replace(/[^A-Z0-9]+/ig, "_");


                  That will result in hello_world_hello_universe






                  share|improve this answer


























                    76












                    76








                    76







                    Your regular expression [^a-zA-Z0-9]s/g says match any character that is not a number or letter followed by a space.



                    Remove the s and you should get what you are after if you want a _ for every special character.



                    var newString = str.replace(/[^A-Z0-9]/ig, "_");


                    That will result in hello_world___hello_universe



                    If you want it to be single underscores use a + to match multiple



                    var newString = str.replace(/[^A-Z0-9]+/ig, "_");


                    That will result in hello_world_hello_universe






                    share|improve this answer













                    Your regular expression [^a-zA-Z0-9]s/g says match any character that is not a number or letter followed by a space.



                    Remove the s and you should get what you are after if you want a _ for every special character.



                    var newString = str.replace(/[^A-Z0-9]/ig, "_");


                    That will result in hello_world___hello_universe



                    If you want it to be single underscores use a + to match multiple



                    var newString = str.replace(/[^A-Z0-9]+/ig, "_");


                    That will result in hello_world_hello_universe







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Oct 22 '12 at 21:36









                    epascarelloepascarello

                    155k14138187




                    155k14138187

























                        3














                        Remove the s from your new regex and it should work - whitespace is already included in "anything but alphanumerics".



                        Note that you may want to add a + after the ] so you don't get sequences of more than one underscore. You can also chain onto .replace(/^_+|_+$/g,'') to trim off underscores at the start or end of the string.






                        share|improve this answer
























                        • Why call separate replace when you can add + in first regex?

                          – MBO
                          Oct 22 '12 at 21:38











                        • Because... good point. I was thinking of something else, I guess. Still, you could chain on a trim-like regex.

                          – Niet the Dark Absol
                          Oct 22 '12 at 21:39
















                        3














                        Remove the s from your new regex and it should work - whitespace is already included in "anything but alphanumerics".



                        Note that you may want to add a + after the ] so you don't get sequences of more than one underscore. You can also chain onto .replace(/^_+|_+$/g,'') to trim off underscores at the start or end of the string.






                        share|improve this answer
























                        • Why call separate replace when you can add + in first regex?

                          – MBO
                          Oct 22 '12 at 21:38











                        • Because... good point. I was thinking of something else, I guess. Still, you could chain on a trim-like regex.

                          – Niet the Dark Absol
                          Oct 22 '12 at 21:39














                        3












                        3








                        3







                        Remove the s from your new regex and it should work - whitespace is already included in "anything but alphanumerics".



                        Note that you may want to add a + after the ] so you don't get sequences of more than one underscore. You can also chain onto .replace(/^_+|_+$/g,'') to trim off underscores at the start or end of the string.






                        share|improve this answer













                        Remove the s from your new regex and it should work - whitespace is already included in "anything but alphanumerics".



                        Note that you may want to add a + after the ] so you don't get sequences of more than one underscore. You can also chain onto .replace(/^_+|_+$/g,'') to trim off underscores at the start or end of the string.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Oct 22 '12 at 21:36









                        Niet the Dark AbsolNiet the Dark Absol

                        261k57365479




                        261k57365479













                        • Why call separate replace when you can add + in first regex?

                          – MBO
                          Oct 22 '12 at 21:38











                        • Because... good point. I was thinking of something else, I guess. Still, you could chain on a trim-like regex.

                          – Niet the Dark Absol
                          Oct 22 '12 at 21:39



















                        • Why call separate replace when you can add + in first regex?

                          – MBO
                          Oct 22 '12 at 21:38











                        • Because... good point. I was thinking of something else, I guess. Still, you could chain on a trim-like regex.

                          – Niet the Dark Absol
                          Oct 22 '12 at 21:39

















                        Why call separate replace when you can add + in first regex?

                        – MBO
                        Oct 22 '12 at 21:38





                        Why call separate replace when you can add + in first regex?

                        – MBO
                        Oct 22 '12 at 21:38













                        Because... good point. I was thinking of something else, I guess. Still, you could chain on a trim-like regex.

                        – Niet the Dark Absol
                        Oct 22 '12 at 21:39





                        Because... good point. I was thinking of something else, I guess. Still, you could chain on a trim-like regex.

                        – Niet the Dark Absol
                        Oct 22 '12 at 21:39











                        1














                        It was not asked precisely to remove accent (only special characters), but I needed to.



                        The solutions givens here works but they don’t remove accent: é, è, etc.



                        So, before doing epascarello’s solution, you can also do:






                        var newString = "développeur & intégrateur";

                        newString = replaceAccents(newString);
                        newString = newString.replace(/[^A-Z0-9]+/ig, "_");
                        alert(newString);

                        /**
                        * Replaces all accented chars with regular ones
                        */
                        function replaceAccents(str) {
                        // Verifies if the String has accents and replace them
                        if (str.search(/[xC0-xFF]/g) > -1) {
                        str = str
                        .replace(/[xC0-xC5]/g, "A")
                        .replace(/[xC6]/g, "AE")
                        .replace(/[xC7]/g, "C")
                        .replace(/[xC8-xCB]/g, "E")
                        .replace(/[xCC-xCF]/g, "I")
                        .replace(/[xD0]/g, "D")
                        .replace(/[xD1]/g, "N")
                        .replace(/[xD2-xD6xD8]/g, "O")
                        .replace(/[xD9-xDC]/g, "U")
                        .replace(/[xDD]/g, "Y")
                        .replace(/[xDE]/g, "P")
                        .replace(/[xE0-xE5]/g, "a")
                        .replace(/[xE6]/g, "ae")
                        .replace(/[xE7]/g, "c")
                        .replace(/[xE8-xEB]/g, "e")
                        .replace(/[xEC-xEF]/g, "i")
                        .replace(/[xF1]/g, "n")
                        .replace(/[xF2-xF6xF8]/g, "o")
                        .replace(/[xF9-xFC]/g, "u")
                        .replace(/[xFE]/g, "p")
                        .replace(/[xFDxFF]/g, "y");
                        }

                        return str;
                        }





                        Source: https://gist.github.com/jonlabelle/5375315






                        share|improve this answer






























                          1














                          It was not asked precisely to remove accent (only special characters), but I needed to.



                          The solutions givens here works but they don’t remove accent: é, è, etc.



                          So, before doing epascarello’s solution, you can also do:






                          var newString = "développeur & intégrateur";

                          newString = replaceAccents(newString);
                          newString = newString.replace(/[^A-Z0-9]+/ig, "_");
                          alert(newString);

                          /**
                          * Replaces all accented chars with regular ones
                          */
                          function replaceAccents(str) {
                          // Verifies if the String has accents and replace them
                          if (str.search(/[xC0-xFF]/g) > -1) {
                          str = str
                          .replace(/[xC0-xC5]/g, "A")
                          .replace(/[xC6]/g, "AE")
                          .replace(/[xC7]/g, "C")
                          .replace(/[xC8-xCB]/g, "E")
                          .replace(/[xCC-xCF]/g, "I")
                          .replace(/[xD0]/g, "D")
                          .replace(/[xD1]/g, "N")
                          .replace(/[xD2-xD6xD8]/g, "O")
                          .replace(/[xD9-xDC]/g, "U")
                          .replace(/[xDD]/g, "Y")
                          .replace(/[xDE]/g, "P")
                          .replace(/[xE0-xE5]/g, "a")
                          .replace(/[xE6]/g, "ae")
                          .replace(/[xE7]/g, "c")
                          .replace(/[xE8-xEB]/g, "e")
                          .replace(/[xEC-xEF]/g, "i")
                          .replace(/[xF1]/g, "n")
                          .replace(/[xF2-xF6xF8]/g, "o")
                          .replace(/[xF9-xFC]/g, "u")
                          .replace(/[xFE]/g, "p")
                          .replace(/[xFDxFF]/g, "y");
                          }

                          return str;
                          }





                          Source: https://gist.github.com/jonlabelle/5375315






                          share|improve this answer




























                            1












                            1








                            1







                            It was not asked precisely to remove accent (only special characters), but I needed to.



                            The solutions givens here works but they don’t remove accent: é, è, etc.



                            So, before doing epascarello’s solution, you can also do:






                            var newString = "développeur & intégrateur";

                            newString = replaceAccents(newString);
                            newString = newString.replace(/[^A-Z0-9]+/ig, "_");
                            alert(newString);

                            /**
                            * Replaces all accented chars with regular ones
                            */
                            function replaceAccents(str) {
                            // Verifies if the String has accents and replace them
                            if (str.search(/[xC0-xFF]/g) > -1) {
                            str = str
                            .replace(/[xC0-xC5]/g, "A")
                            .replace(/[xC6]/g, "AE")
                            .replace(/[xC7]/g, "C")
                            .replace(/[xC8-xCB]/g, "E")
                            .replace(/[xCC-xCF]/g, "I")
                            .replace(/[xD0]/g, "D")
                            .replace(/[xD1]/g, "N")
                            .replace(/[xD2-xD6xD8]/g, "O")
                            .replace(/[xD9-xDC]/g, "U")
                            .replace(/[xDD]/g, "Y")
                            .replace(/[xDE]/g, "P")
                            .replace(/[xE0-xE5]/g, "a")
                            .replace(/[xE6]/g, "ae")
                            .replace(/[xE7]/g, "c")
                            .replace(/[xE8-xEB]/g, "e")
                            .replace(/[xEC-xEF]/g, "i")
                            .replace(/[xF1]/g, "n")
                            .replace(/[xF2-xF6xF8]/g, "o")
                            .replace(/[xF9-xFC]/g, "u")
                            .replace(/[xFE]/g, "p")
                            .replace(/[xFDxFF]/g, "y");
                            }

                            return str;
                            }





                            Source: https://gist.github.com/jonlabelle/5375315






                            share|improve this answer















                            It was not asked precisely to remove accent (only special characters), but I needed to.



                            The solutions givens here works but they don’t remove accent: é, è, etc.



                            So, before doing epascarello’s solution, you can also do:






                            var newString = "développeur & intégrateur";

                            newString = replaceAccents(newString);
                            newString = newString.replace(/[^A-Z0-9]+/ig, "_");
                            alert(newString);

                            /**
                            * Replaces all accented chars with regular ones
                            */
                            function replaceAccents(str) {
                            // Verifies if the String has accents and replace them
                            if (str.search(/[xC0-xFF]/g) > -1) {
                            str = str
                            .replace(/[xC0-xC5]/g, "A")
                            .replace(/[xC6]/g, "AE")
                            .replace(/[xC7]/g, "C")
                            .replace(/[xC8-xCB]/g, "E")
                            .replace(/[xCC-xCF]/g, "I")
                            .replace(/[xD0]/g, "D")
                            .replace(/[xD1]/g, "N")
                            .replace(/[xD2-xD6xD8]/g, "O")
                            .replace(/[xD9-xDC]/g, "U")
                            .replace(/[xDD]/g, "Y")
                            .replace(/[xDE]/g, "P")
                            .replace(/[xE0-xE5]/g, "a")
                            .replace(/[xE6]/g, "ae")
                            .replace(/[xE7]/g, "c")
                            .replace(/[xE8-xEB]/g, "e")
                            .replace(/[xEC-xEF]/g, "i")
                            .replace(/[xF1]/g, "n")
                            .replace(/[xF2-xF6xF8]/g, "o")
                            .replace(/[xF9-xFC]/g, "u")
                            .replace(/[xFE]/g, "p")
                            .replace(/[xFDxFF]/g, "y");
                            }

                            return str;
                            }





                            Source: https://gist.github.com/jonlabelle/5375315






                            var newString = "développeur & intégrateur";

                            newString = replaceAccents(newString);
                            newString = newString.replace(/[^A-Z0-9]+/ig, "_");
                            alert(newString);

                            /**
                            * Replaces all accented chars with regular ones
                            */
                            function replaceAccents(str) {
                            // Verifies if the String has accents and replace them
                            if (str.search(/[xC0-xFF]/g) > -1) {
                            str = str
                            .replace(/[xC0-xC5]/g, "A")
                            .replace(/[xC6]/g, "AE")
                            .replace(/[xC7]/g, "C")
                            .replace(/[xC8-xCB]/g, "E")
                            .replace(/[xCC-xCF]/g, "I")
                            .replace(/[xD0]/g, "D")
                            .replace(/[xD1]/g, "N")
                            .replace(/[xD2-xD6xD8]/g, "O")
                            .replace(/[xD9-xDC]/g, "U")
                            .replace(/[xDD]/g, "Y")
                            .replace(/[xDE]/g, "P")
                            .replace(/[xE0-xE5]/g, "a")
                            .replace(/[xE6]/g, "ae")
                            .replace(/[xE7]/g, "c")
                            .replace(/[xE8-xEB]/g, "e")
                            .replace(/[xEC-xEF]/g, "i")
                            .replace(/[xF1]/g, "n")
                            .replace(/[xF2-xF6xF8]/g, "o")
                            .replace(/[xF9-xFC]/g, "u")
                            .replace(/[xFE]/g, "p")
                            .replace(/[xFDxFF]/g, "y");
                            }

                            return str;
                            }





                            var newString = "développeur & intégrateur";

                            newString = replaceAccents(newString);
                            newString = newString.replace(/[^A-Z0-9]+/ig, "_");
                            alert(newString);

                            /**
                            * Replaces all accented chars with regular ones
                            */
                            function replaceAccents(str) {
                            // Verifies if the String has accents and replace them
                            if (str.search(/[xC0-xFF]/g) > -1) {
                            str = str
                            .replace(/[xC0-xC5]/g, "A")
                            .replace(/[xC6]/g, "AE")
                            .replace(/[xC7]/g, "C")
                            .replace(/[xC8-xCB]/g, "E")
                            .replace(/[xCC-xCF]/g, "I")
                            .replace(/[xD0]/g, "D")
                            .replace(/[xD1]/g, "N")
                            .replace(/[xD2-xD6xD8]/g, "O")
                            .replace(/[xD9-xDC]/g, "U")
                            .replace(/[xDD]/g, "Y")
                            .replace(/[xDE]/g, "P")
                            .replace(/[xE0-xE5]/g, "a")
                            .replace(/[xE6]/g, "ae")
                            .replace(/[xE7]/g, "c")
                            .replace(/[xE8-xEB]/g, "e")
                            .replace(/[xEC-xEF]/g, "i")
                            .replace(/[xF1]/g, "n")
                            .replace(/[xF2-xF6xF8]/g, "o")
                            .replace(/[xF9-xFC]/g, "u")
                            .replace(/[xFE]/g, "p")
                            .replace(/[xFDxFF]/g, "y");
                            }

                            return str;
                            }






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Oct 11 '18 at 18:11









                            Peter Mortensen

                            14k1987114




                            14k1987114










                            answered Jul 7 '18 at 12:15









                            Sébastien GicquelSébastien Gicquel

                            2,25633362




                            2,25633362























                                0














                                If you have a text as



                                var sampleText ="ä_öü_ßÄ_ TESTED Ö_Ü!@#$%^&())(&&++===.XYZ"



                                To replace all special character (!@#$%^&())(&&++= ==.) without replacing the characters(including umlaut)



                                Use below regex



                                sampleText = sampleText.replace(/[`~!@#$%^&*()|+-=?;:'",.<>{}/s]/gi,'');



                                OUTPUT : sampleText = "ä_öü_ßÄ____TESTED_Ö_Ü_____________________XYZ"



                                This would replace all with an underscore which is provided as second argument to the replace function.You can add whatever you want as per your requirement






                                share|improve this answer




























                                  0














                                  If you have a text as



                                  var sampleText ="ä_öü_ßÄ_ TESTED Ö_Ü!@#$%^&())(&&++===.XYZ"



                                  To replace all special character (!@#$%^&())(&&++= ==.) without replacing the characters(including umlaut)



                                  Use below regex



                                  sampleText = sampleText.replace(/[`~!@#$%^&*()|+-=?;:'",.<>{}/s]/gi,'');



                                  OUTPUT : sampleText = "ä_öü_ßÄ____TESTED_Ö_Ü_____________________XYZ"



                                  This would replace all with an underscore which is provided as second argument to the replace function.You can add whatever you want as per your requirement






                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    If you have a text as



                                    var sampleText ="ä_öü_ßÄ_ TESTED Ö_Ü!@#$%^&())(&&++===.XYZ"



                                    To replace all special character (!@#$%^&())(&&++= ==.) without replacing the characters(including umlaut)



                                    Use below regex



                                    sampleText = sampleText.replace(/[`~!@#$%^&*()|+-=?;:'",.<>{}/s]/gi,'');



                                    OUTPUT : sampleText = "ä_öü_ßÄ____TESTED_Ö_Ü_____________________XYZ"



                                    This would replace all with an underscore which is provided as second argument to the replace function.You can add whatever you want as per your requirement






                                    share|improve this answer













                                    If you have a text as



                                    var sampleText ="ä_öü_ßÄ_ TESTED Ö_Ü!@#$%^&())(&&++===.XYZ"



                                    To replace all special character (!@#$%^&())(&&++= ==.) without replacing the characters(including umlaut)



                                    Use below regex



                                    sampleText = sampleText.replace(/[`~!@#$%^&*()|+-=?;:'",.<>{}/s]/gi,'');



                                    OUTPUT : sampleText = "ä_öü_ßÄ____TESTED_Ö_Ü_____________________XYZ"



                                    This would replace all with an underscore which is provided as second argument to the replace function.You can add whatever you want as per your requirement







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 23 '18 at 7:40









                                    SitaramSitaram

                                    10923




                                    10923






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f13020246%2fremove-special-symbols-and-extra-spaces-and-replace-with-underscore-using-the-re%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        How to change which sound is reproduced for terminal bell?

                                        Can I use Tabulator js library in my java Spring + Thymeleaf project?

                                        Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents