How to trim multiple characters?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite












I have a string as follows



const example = ' ( some string ()() here ) ';


If I trim the string with



example.trim()


it will give me the output: ( some string ()() here )



But I want the output some string ()() here. How to achieve that?






const example = ' ( some string ()() here ) ';
console.log(example.trim());












share|improve this question



























    up vote
    6
    down vote

    favorite












    I have a string as follows



    const example = ' ( some string ()() here ) ';


    If I trim the string with



    example.trim()


    it will give me the output: ( some string ()() here )



    But I want the output some string ()() here. How to achieve that?






    const example = ' ( some string ()() here ) ';
    console.log(example.trim());












    share|improve this question

























      up vote
      6
      down vote

      favorite









      up vote
      6
      down vote

      favorite











      I have a string as follows



      const example = ' ( some string ()() here ) ';


      If I trim the string with



      example.trim()


      it will give me the output: ( some string ()() here )



      But I want the output some string ()() here. How to achieve that?






      const example = ' ( some string ()() here ) ';
      console.log(example.trim());












      share|improve this question















      I have a string as follows



      const example = ' ( some string ()() here ) ';


      If I trim the string with



      example.trim()


      it will give me the output: ( some string ()() here )



      But I want the output some string ()() here. How to achieve that?






      const example = ' ( some string ()() here ) ';
      console.log(example.trim());








      const example = ' ( some string ()() here ) ';
      console.log(example.trim());





      const example = ' ( some string ()() here ) ';
      console.log(example.trim());






      javascript string trim






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 hours ago

























      asked 2 hours ago









      sertsedat

      2,1141231




      2,1141231






















          5 Answers
          5






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          You can use a regex for leading and trailing space/brackets:






          function grabText(str) 
          return str.replace(/^s+(s+(.*)s+)s+$/g,"$1");


          console.log(
          grabText(' ( some (string) here ) ')
          )
          console.log(
          grabText(' ( some string ()() here ) ')
          )








          share|improve this answer






















          • thanks for this, but this is a bad assumption. the string inside can also contain (). That's why I'm trying to trim. Just updated my question to have better clarity. Sorry about that
            – sertsedat
            2 hours ago











          • Well, by definition trim only removes whitespace. Occasionally you'd find some trim functionality that does more - actually the other day I found out C# does that, where you can define which characters to remove from the beginning and the end but the generally understood definition of trim is for whitespace only.
            – vlaz
            2 hours ago










          • Please see my updated example which includes inner brackets
            – mplungjan
            2 hours ago







          • 1




            Well, you can write your own function that does it without regex: take parameters for what to strip, go from the beginning and end of the string and skip any strippable character, then return the inner part. But it'd be a bit of a wasted effort, as regex+replace is readily available.
            – vlaz
            2 hours ago






          • 1




            @sertsedat - my latest version handles both your cases
            – mplungjan
            2 hours ago

















          up vote
          3
          down vote













          You can use the regex to get the matched string, The below regex, matches the first character followed by characters or whitespaces and ending with a alphanumberic character






          const example = ' ( some (string) ()()here ) ';
          console.log(example.match(/(w[ws.(.*)]+)w/g));








          share|improve this answer






















          • @mplungjan, yes that won't work. Ahh, OP updates his question
            – Shubham Khatri
            2 hours ago











          • well this also works. sorry about that. I guess you didn't see edited question. thanks for the answer
            – sertsedat
            1 hour ago

















          up vote
          1
          down vote
















           const str = ' ( some ( string ) here ) '.replace(/^s+(s+(.*)s+)s+$/g,'$1');
          console.log(str);








          share|improve this answer


















          • 1




            Why trim after using a regex? You can let the regex do the trim too
            – mplungjan
            1 hour ago










          • yes yes :) I was writing an other solution so it is fixed now
            – khaled_bhar
            1 hour ago










          • Your solution is now identical to mine - exactly the same
            – mplungjan
            1 hour ago


















          up vote
          0
          down vote













          You could use a recursive method and specify the number of times you wish to trim the string. This will also work with things other than round brackets, for instance, square brackets:






          const example = ' ( some string ()() here ) ';
          const exampleTwo = ' [ This, is [some] text ] ';

          function trim_factor(str, times)
          if(times == 0)
          return str;

          str = str.trim();
          return str.charAt(0) + trim_factor(str.substr(1, str.length-2), times-1) + str.charAt(str.length-1);


          console.log(trim_factor(example, 2));
          console.log(trim_factor(exampleTwo, 2));








          share|improve this answer





























            up vote
            -2
            down vote













            You should to replace the "(" and ")" characters as in the following example:






            var str = " ( Some string ) ";
            str.trim();
            str = str.replace("(", "");
            str = str.replace(")", "");








            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',
              convertImagesToLinks: true,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              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%2f52532360%2fhow-to-trim-multiple-characters%23new-answer', 'question_page');

              );

              Post as a guest






























              5 Answers
              5






              active

              oldest

              votes








              5 Answers
              5






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              4
              down vote



              accepted










              You can use a regex for leading and trailing space/brackets:






              function grabText(str) 
              return str.replace(/^s+(s+(.*)s+)s+$/g,"$1");


              console.log(
              grabText(' ( some (string) here ) ')
              )
              console.log(
              grabText(' ( some string ()() here ) ')
              )








              share|improve this answer






















              • thanks for this, but this is a bad assumption. the string inside can also contain (). That's why I'm trying to trim. Just updated my question to have better clarity. Sorry about that
                – sertsedat
                2 hours ago











              • Well, by definition trim only removes whitespace. Occasionally you'd find some trim functionality that does more - actually the other day I found out C# does that, where you can define which characters to remove from the beginning and the end but the generally understood definition of trim is for whitespace only.
                – vlaz
                2 hours ago










              • Please see my updated example which includes inner brackets
                – mplungjan
                2 hours ago







              • 1




                Well, you can write your own function that does it without regex: take parameters for what to strip, go from the beginning and end of the string and skip any strippable character, then return the inner part. But it'd be a bit of a wasted effort, as regex+replace is readily available.
                – vlaz
                2 hours ago






              • 1




                @sertsedat - my latest version handles both your cases
                – mplungjan
                2 hours ago














              up vote
              4
              down vote



              accepted










              You can use a regex for leading and trailing space/brackets:






              function grabText(str) 
              return str.replace(/^s+(s+(.*)s+)s+$/g,"$1");


              console.log(
              grabText(' ( some (string) here ) ')
              )
              console.log(
              grabText(' ( some string ()() here ) ')
              )








              share|improve this answer






















              • thanks for this, but this is a bad assumption. the string inside can also contain (). That's why I'm trying to trim. Just updated my question to have better clarity. Sorry about that
                – sertsedat
                2 hours ago











              • Well, by definition trim only removes whitespace. Occasionally you'd find some trim functionality that does more - actually the other day I found out C# does that, where you can define which characters to remove from the beginning and the end but the generally understood definition of trim is for whitespace only.
                – vlaz
                2 hours ago










              • Please see my updated example which includes inner brackets
                – mplungjan
                2 hours ago







              • 1




                Well, you can write your own function that does it without regex: take parameters for what to strip, go from the beginning and end of the string and skip any strippable character, then return the inner part. But it'd be a bit of a wasted effort, as regex+replace is readily available.
                – vlaz
                2 hours ago






              • 1




                @sertsedat - my latest version handles both your cases
                – mplungjan
                2 hours ago












              up vote
              4
              down vote



              accepted







              up vote
              4
              down vote



              accepted






              You can use a regex for leading and trailing space/brackets:






              function grabText(str) 
              return str.replace(/^s+(s+(.*)s+)s+$/g,"$1");


              console.log(
              grabText(' ( some (string) here ) ')
              )
              console.log(
              grabText(' ( some string ()() here ) ')
              )








              share|improve this answer














              You can use a regex for leading and trailing space/brackets:






              function grabText(str) 
              return str.replace(/^s+(s+(.*)s+)s+$/g,"$1");


              console.log(
              grabText(' ( some (string) here ) ')
              )
              console.log(
              grabText(' ( some string ()() here ) ')
              )








              function grabText(str) 
              return str.replace(/^s+(s+(.*)s+)s+$/g,"$1");


              console.log(
              grabText(' ( some (string) here ) ')
              )
              console.log(
              grabText(' ( some string ()() here ) ')
              )





              function grabText(str) 
              return str.replace(/^s+(s+(.*)s+)s+$/g,"$1");


              console.log(
              grabText(' ( some (string) here ) ')
              )
              console.log(
              grabText(' ( some string ()() here ) ')
              )






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 2 hours ago

























              answered 2 hours ago









              mplungjan

              84k20120179




              84k20120179











              • thanks for this, but this is a bad assumption. the string inside can also contain (). That's why I'm trying to trim. Just updated my question to have better clarity. Sorry about that
                – sertsedat
                2 hours ago











              • Well, by definition trim only removes whitespace. Occasionally you'd find some trim functionality that does more - actually the other day I found out C# does that, where you can define which characters to remove from the beginning and the end but the generally understood definition of trim is for whitespace only.
                – vlaz
                2 hours ago










              • Please see my updated example which includes inner brackets
                – mplungjan
                2 hours ago







              • 1




                Well, you can write your own function that does it without regex: take parameters for what to strip, go from the beginning and end of the string and skip any strippable character, then return the inner part. But it'd be a bit of a wasted effort, as regex+replace is readily available.
                – vlaz
                2 hours ago






              • 1




                @sertsedat - my latest version handles both your cases
                – mplungjan
                2 hours ago
















              • thanks for this, but this is a bad assumption. the string inside can also contain (). That's why I'm trying to trim. Just updated my question to have better clarity. Sorry about that
                – sertsedat
                2 hours ago











              • Well, by definition trim only removes whitespace. Occasionally you'd find some trim functionality that does more - actually the other day I found out C# does that, where you can define which characters to remove from the beginning and the end but the generally understood definition of trim is for whitespace only.
                – vlaz
                2 hours ago










              • Please see my updated example which includes inner brackets
                – mplungjan
                2 hours ago







              • 1




                Well, you can write your own function that does it without regex: take parameters for what to strip, go from the beginning and end of the string and skip any strippable character, then return the inner part. But it'd be a bit of a wasted effort, as regex+replace is readily available.
                – vlaz
                2 hours ago






              • 1




                @sertsedat - my latest version handles both your cases
                – mplungjan
                2 hours ago















              thanks for this, but this is a bad assumption. the string inside can also contain (). That's why I'm trying to trim. Just updated my question to have better clarity. Sorry about that
              – sertsedat
              2 hours ago





              thanks for this, but this is a bad assumption. the string inside can also contain (). That's why I'm trying to trim. Just updated my question to have better clarity. Sorry about that
              – sertsedat
              2 hours ago













              Well, by definition trim only removes whitespace. Occasionally you'd find some trim functionality that does more - actually the other day I found out C# does that, where you can define which characters to remove from the beginning and the end but the generally understood definition of trim is for whitespace only.
              – vlaz
              2 hours ago




              Well, by definition trim only removes whitespace. Occasionally you'd find some trim functionality that does more - actually the other day I found out C# does that, where you can define which characters to remove from the beginning and the end but the generally understood definition of trim is for whitespace only.
              – vlaz
              2 hours ago












              Please see my updated example which includes inner brackets
              – mplungjan
              2 hours ago





              Please see my updated example which includes inner brackets
              – mplungjan
              2 hours ago





              1




              1




              Well, you can write your own function that does it without regex: take parameters for what to strip, go from the beginning and end of the string and skip any strippable character, then return the inner part. But it'd be a bit of a wasted effort, as regex+replace is readily available.
              – vlaz
              2 hours ago




              Well, you can write your own function that does it without regex: take parameters for what to strip, go from the beginning and end of the string and skip any strippable character, then return the inner part. But it'd be a bit of a wasted effort, as regex+replace is readily available.
              – vlaz
              2 hours ago




              1




              1




              @sertsedat - my latest version handles both your cases
              – mplungjan
              2 hours ago




              @sertsedat - my latest version handles both your cases
              – mplungjan
              2 hours ago












              up vote
              3
              down vote













              You can use the regex to get the matched string, The below regex, matches the first character followed by characters or whitespaces and ending with a alphanumberic character






              const example = ' ( some (string) ()()here ) ';
              console.log(example.match(/(w[ws.(.*)]+)w/g));








              share|improve this answer






















              • @mplungjan, yes that won't work. Ahh, OP updates his question
                – Shubham Khatri
                2 hours ago











              • well this also works. sorry about that. I guess you didn't see edited question. thanks for the answer
                – sertsedat
                1 hour ago














              up vote
              3
              down vote













              You can use the regex to get the matched string, The below regex, matches the first character followed by characters or whitespaces and ending with a alphanumberic character






              const example = ' ( some (string) ()()here ) ';
              console.log(example.match(/(w[ws.(.*)]+)w/g));








              share|improve this answer






















              • @mplungjan, yes that won't work. Ahh, OP updates his question
                – Shubham Khatri
                2 hours ago











              • well this also works. sorry about that. I guess you didn't see edited question. thanks for the answer
                – sertsedat
                1 hour ago












              up vote
              3
              down vote










              up vote
              3
              down vote









              You can use the regex to get the matched string, The below regex, matches the first character followed by characters or whitespaces and ending with a alphanumberic character






              const example = ' ( some (string) ()()here ) ';
              console.log(example.match(/(w[ws.(.*)]+)w/g));








              share|improve this answer














              You can use the regex to get the matched string, The below regex, matches the first character followed by characters or whitespaces and ending with a alphanumberic character






              const example = ' ( some (string) ()()here ) ';
              console.log(example.match(/(w[ws.(.*)]+)w/g));








              const example = ' ( some (string) ()()here ) ';
              console.log(example.match(/(w[ws.(.*)]+)w/g));





              const example = ' ( some (string) ()()here ) ';
              console.log(example.match(/(w[ws.(.*)]+)w/g));






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 2 hours ago

























              answered 2 hours ago









              Shubham Khatri

              66.9k1376117




              66.9k1376117











              • @mplungjan, yes that won't work. Ahh, OP updates his question
                – Shubham Khatri
                2 hours ago











              • well this also works. sorry about that. I guess you didn't see edited question. thanks for the answer
                – sertsedat
                1 hour ago
















              • @mplungjan, yes that won't work. Ahh, OP updates his question
                – Shubham Khatri
                2 hours ago











              • well this also works. sorry about that. I guess you didn't see edited question. thanks for the answer
                – sertsedat
                1 hour ago















              @mplungjan, yes that won't work. Ahh, OP updates his question
              – Shubham Khatri
              2 hours ago





              @mplungjan, yes that won't work. Ahh, OP updates his question
              – Shubham Khatri
              2 hours ago













              well this also works. sorry about that. I guess you didn't see edited question. thanks for the answer
              – sertsedat
              1 hour ago




              well this also works. sorry about that. I guess you didn't see edited question. thanks for the answer
              – sertsedat
              1 hour ago










              up vote
              1
              down vote
















               const str = ' ( some ( string ) here ) '.replace(/^s+(s+(.*)s+)s+$/g,'$1');
              console.log(str);








              share|improve this answer


















              • 1




                Why trim after using a regex? You can let the regex do the trim too
                – mplungjan
                1 hour ago










              • yes yes :) I was writing an other solution so it is fixed now
                – khaled_bhar
                1 hour ago










              • Your solution is now identical to mine - exactly the same
                – mplungjan
                1 hour ago















              up vote
              1
              down vote
















               const str = ' ( some ( string ) here ) '.replace(/^s+(s+(.*)s+)s+$/g,'$1');
              console.log(str);








              share|improve this answer


















              • 1




                Why trim after using a regex? You can let the regex do the trim too
                – mplungjan
                1 hour ago










              • yes yes :) I was writing an other solution so it is fixed now
                – khaled_bhar
                1 hour ago










              • Your solution is now identical to mine - exactly the same
                – mplungjan
                1 hour ago













              up vote
              1
              down vote










              up vote
              1
              down vote












               const str = ' ( some ( string ) here ) '.replace(/^s+(s+(.*)s+)s+$/g,'$1');
              console.log(str);








              share|improve this answer

















               const str = ' ( some ( string ) here ) '.replace(/^s+(s+(.*)s+)s+$/g,'$1');
              console.log(str);








               const str = ' ( some ( string ) here ) '.replace(/^s+(s+(.*)s+)s+$/g,'$1');
              console.log(str);





               const str = ' ( some ( string ) here ) '.replace(/^s+(s+(.*)s+)s+$/g,'$1');
              console.log(str);






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 1 hour ago

























              answered 2 hours ago









              khaled_bhar

              114118




              114118







              • 1




                Why trim after using a regex? You can let the regex do the trim too
                – mplungjan
                1 hour ago










              • yes yes :) I was writing an other solution so it is fixed now
                – khaled_bhar
                1 hour ago










              • Your solution is now identical to mine - exactly the same
                – mplungjan
                1 hour ago













              • 1




                Why trim after using a regex? You can let the regex do the trim too
                – mplungjan
                1 hour ago










              • yes yes :) I was writing an other solution so it is fixed now
                – khaled_bhar
                1 hour ago










              • Your solution is now identical to mine - exactly the same
                – mplungjan
                1 hour ago








              1




              1




              Why trim after using a regex? You can let the regex do the trim too
              – mplungjan
              1 hour ago




              Why trim after using a regex? You can let the regex do the trim too
              – mplungjan
              1 hour ago












              yes yes :) I was writing an other solution so it is fixed now
              – khaled_bhar
              1 hour ago




              yes yes :) I was writing an other solution so it is fixed now
              – khaled_bhar
              1 hour ago












              Your solution is now identical to mine - exactly the same
              – mplungjan
              1 hour ago





              Your solution is now identical to mine - exactly the same
              – mplungjan
              1 hour ago











              up vote
              0
              down vote













              You could use a recursive method and specify the number of times you wish to trim the string. This will also work with things other than round brackets, for instance, square brackets:






              const example = ' ( some string ()() here ) ';
              const exampleTwo = ' [ This, is [some] text ] ';

              function trim_factor(str, times)
              if(times == 0)
              return str;

              str = str.trim();
              return str.charAt(0) + trim_factor(str.substr(1, str.length-2), times-1) + str.charAt(str.length-1);


              console.log(trim_factor(example, 2));
              console.log(trim_factor(exampleTwo, 2));








              share|improve this answer


























                up vote
                0
                down vote













                You could use a recursive method and specify the number of times you wish to trim the string. This will also work with things other than round brackets, for instance, square brackets:






                const example = ' ( some string ()() here ) ';
                const exampleTwo = ' [ This, is [some] text ] ';

                function trim_factor(str, times)
                if(times == 0)
                return str;

                str = str.trim();
                return str.charAt(0) + trim_factor(str.substr(1, str.length-2), times-1) + str.charAt(str.length-1);


                console.log(trim_factor(example, 2));
                console.log(trim_factor(exampleTwo, 2));








                share|improve this answer
























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  You could use a recursive method and specify the number of times you wish to trim the string. This will also work with things other than round brackets, for instance, square brackets:






                  const example = ' ( some string ()() here ) ';
                  const exampleTwo = ' [ This, is [some] text ] ';

                  function trim_factor(str, times)
                  if(times == 0)
                  return str;

                  str = str.trim();
                  return str.charAt(0) + trim_factor(str.substr(1, str.length-2), times-1) + str.charAt(str.length-1);


                  console.log(trim_factor(example, 2));
                  console.log(trim_factor(exampleTwo, 2));








                  share|improve this answer














                  You could use a recursive method and specify the number of times you wish to trim the string. This will also work with things other than round brackets, for instance, square brackets:






                  const example = ' ( some string ()() here ) ';
                  const exampleTwo = ' [ This, is [some] text ] ';

                  function trim_factor(str, times)
                  if(times == 0)
                  return str;

                  str = str.trim();
                  return str.charAt(0) + trim_factor(str.substr(1, str.length-2), times-1) + str.charAt(str.length-1);


                  console.log(trim_factor(example, 2));
                  console.log(trim_factor(exampleTwo, 2));








                  const example = ' ( some string ()() here ) ';
                  const exampleTwo = ' [ This, is [some] text ] ';

                  function trim_factor(str, times)
                  if(times == 0)
                  return str;

                  str = str.trim();
                  return str.charAt(0) + trim_factor(str.substr(1, str.length-2), times-1) + str.charAt(str.length-1);


                  console.log(trim_factor(example, 2));
                  console.log(trim_factor(exampleTwo, 2));





                  const example = ' ( some string ()() here ) ';
                  const exampleTwo = ' [ This, is [some] text ] ';

                  function trim_factor(str, times)
                  if(times == 0)
                  return str;

                  str = str.trim();
                  return str.charAt(0) + trim_factor(str.substr(1, str.length-2), times-1) + str.charAt(str.length-1);


                  console.log(trim_factor(example, 2));
                  console.log(trim_factor(exampleTwo, 2));






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 1 hour ago

























                  answered 2 hours ago









                  Nick Parsons

                  1,603415




                  1,603415




















                      up vote
                      -2
                      down vote













                      You should to replace the "(" and ")" characters as in the following example:






                      var str = " ( Some string ) ";
                      str.trim();
                      str = str.replace("(", "");
                      str = str.replace(")", "");








                      share|improve this answer
























                        up vote
                        -2
                        down vote













                        You should to replace the "(" and ")" characters as in the following example:






                        var str = " ( Some string ) ";
                        str.trim();
                        str = str.replace("(", "");
                        str = str.replace(")", "");








                        share|improve this answer






















                          up vote
                          -2
                          down vote










                          up vote
                          -2
                          down vote









                          You should to replace the "(" and ")" characters as in the following example:






                          var str = " ( Some string ) ";
                          str.trim();
                          str = str.replace("(", "");
                          str = str.replace(")", "");








                          share|improve this answer












                          You should to replace the "(" and ")" characters as in the following example:






                          var str = " ( Some string ) ";
                          str.trim();
                          str = str.replace("(", "");
                          str = str.replace(")", "");








                          var str = " ( Some string ) ";
                          str.trim();
                          str = str.replace("(", "");
                          str = str.replace(")", "");





                          var str = " ( Some string ) ";
                          str.trim();
                          str = str.replace("(", "");
                          str = str.replace(")", "");






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 2 hours ago









                          Sh. Pavel

                          769317




                          769317



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52532360%2fhow-to-trim-multiple-characters%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Comments

                              Popular posts from this blog

                              What does second last employer means? [closed]

                              List of Gilmore Girls characters

                              Confectionery