How can I get the value from a string that has the values inside a square bracket []?










1















I have a string that comes to me like this: "[value1][value2]"



How can I get the values that are inside the square brackets?
NOTE: if the string is like this "[value2]" the first bracket that has a space must return a "" to me...



I have been trying a lot of regex and split but none workd.



this is the last I tried:



var pattern = /[([^]]*)]/g; 
var res = pattern.exec(datos[0].title);


Another one I tried is:



var res = datos[0].title.match(/^.*?[([^]]*)].*?[([^]]*)]/gm);


but none do what I need...



I'm trying to find a way "that does it all" a regex that gets anything inside the Square brackets (even white spaces)










share|improve this question
























  • What do you know about the values in the brackets? If you know that they won't contain square brackets, you could just use split and then trim off the closing bracket.

    – Harry Cutts
    Nov 14 '18 at 0:08











  • Well Jeff, I have seen many regex that helps, but theyreturn the bracket, this is the last I tried: var pattern = /[([^]]*)]/g; var res = pattern.exec(datos[0].title); Another one I tried is: var res = datos[0].title.match(/^.*?[([^]]*)].*?[([^]]*)]/gm); but none do what I need...

    – Hexxul
    Nov 14 '18 at 0:11












  • Put it on the post. Please look at stackoverflow.com/help/how-to-ask

    – Abana Clara
    Nov 14 '18 at 0:14






  • 2





    Possible duplicate of Regular expression to extract text between square brackets

    – slider
    Nov 14 '18 at 0:16











  • Abana, I don't know why it is relevant to post code that I've tested and does not do what It is required, but updated my post. I'm trying to find a way "that does it all" a regex that gets anything inside the Square brackets (even white spaces)

    – Hexxul
    Nov 14 '18 at 0:18















1















I have a string that comes to me like this: "[value1][value2]"



How can I get the values that are inside the square brackets?
NOTE: if the string is like this "[value2]" the first bracket that has a space must return a "" to me...



I have been trying a lot of regex and split but none workd.



this is the last I tried:



var pattern = /[([^]]*)]/g; 
var res = pattern.exec(datos[0].title);


Another one I tried is:



var res = datos[0].title.match(/^.*?[([^]]*)].*?[([^]]*)]/gm);


but none do what I need...



I'm trying to find a way "that does it all" a regex that gets anything inside the Square brackets (even white spaces)










share|improve this question
























  • What do you know about the values in the brackets? If you know that they won't contain square brackets, you could just use split and then trim off the closing bracket.

    – Harry Cutts
    Nov 14 '18 at 0:08











  • Well Jeff, I have seen many regex that helps, but theyreturn the bracket, this is the last I tried: var pattern = /[([^]]*)]/g; var res = pattern.exec(datos[0].title); Another one I tried is: var res = datos[0].title.match(/^.*?[([^]]*)].*?[([^]]*)]/gm); but none do what I need...

    – Hexxul
    Nov 14 '18 at 0:11












  • Put it on the post. Please look at stackoverflow.com/help/how-to-ask

    – Abana Clara
    Nov 14 '18 at 0:14






  • 2





    Possible duplicate of Regular expression to extract text between square brackets

    – slider
    Nov 14 '18 at 0:16











  • Abana, I don't know why it is relevant to post code that I've tested and does not do what It is required, but updated my post. I'm trying to find a way "that does it all" a regex that gets anything inside the Square brackets (even white spaces)

    – Hexxul
    Nov 14 '18 at 0:18













1












1








1








I have a string that comes to me like this: "[value1][value2]"



How can I get the values that are inside the square brackets?
NOTE: if the string is like this "[value2]" the first bracket that has a space must return a "" to me...



I have been trying a lot of regex and split but none workd.



this is the last I tried:



var pattern = /[([^]]*)]/g; 
var res = pattern.exec(datos[0].title);


Another one I tried is:



var res = datos[0].title.match(/^.*?[([^]]*)].*?[([^]]*)]/gm);


but none do what I need...



I'm trying to find a way "that does it all" a regex that gets anything inside the Square brackets (even white spaces)










share|improve this question
















I have a string that comes to me like this: "[value1][value2]"



How can I get the values that are inside the square brackets?
NOTE: if the string is like this "[value2]" the first bracket that has a space must return a "" to me...



I have been trying a lot of regex and split but none workd.



this is the last I tried:



var pattern = /[([^]]*)]/g; 
var res = pattern.exec(datos[0].title);


Another one I tried is:



var res = datos[0].title.match(/^.*?[([^]]*)].*?[([^]]*)]/gm);


but none do what I need...



I'm trying to find a way "that does it all" a regex that gets anything inside the Square brackets (even white spaces)







javascript split






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 0:19









Jeff

6,34911025




6,34911025










asked Nov 14 '18 at 0:01









HexxulHexxul

8013




8013












  • What do you know about the values in the brackets? If you know that they won't contain square brackets, you could just use split and then trim off the closing bracket.

    – Harry Cutts
    Nov 14 '18 at 0:08











  • Well Jeff, I have seen many regex that helps, but theyreturn the bracket, this is the last I tried: var pattern = /[([^]]*)]/g; var res = pattern.exec(datos[0].title); Another one I tried is: var res = datos[0].title.match(/^.*?[([^]]*)].*?[([^]]*)]/gm); but none do what I need...

    – Hexxul
    Nov 14 '18 at 0:11












  • Put it on the post. Please look at stackoverflow.com/help/how-to-ask

    – Abana Clara
    Nov 14 '18 at 0:14






  • 2





    Possible duplicate of Regular expression to extract text between square brackets

    – slider
    Nov 14 '18 at 0:16











  • Abana, I don't know why it is relevant to post code that I've tested and does not do what It is required, but updated my post. I'm trying to find a way "that does it all" a regex that gets anything inside the Square brackets (even white spaces)

    – Hexxul
    Nov 14 '18 at 0:18

















  • What do you know about the values in the brackets? If you know that they won't contain square brackets, you could just use split and then trim off the closing bracket.

    – Harry Cutts
    Nov 14 '18 at 0:08











  • Well Jeff, I have seen many regex that helps, but theyreturn the bracket, this is the last I tried: var pattern = /[([^]]*)]/g; var res = pattern.exec(datos[0].title); Another one I tried is: var res = datos[0].title.match(/^.*?[([^]]*)].*?[([^]]*)]/gm); but none do what I need...

    – Hexxul
    Nov 14 '18 at 0:11












  • Put it on the post. Please look at stackoverflow.com/help/how-to-ask

    – Abana Clara
    Nov 14 '18 at 0:14






  • 2





    Possible duplicate of Regular expression to extract text between square brackets

    – slider
    Nov 14 '18 at 0:16











  • Abana, I don't know why it is relevant to post code that I've tested and does not do what It is required, but updated my post. I'm trying to find a way "that does it all" a regex that gets anything inside the Square brackets (even white spaces)

    – Hexxul
    Nov 14 '18 at 0:18
















What do you know about the values in the brackets? If you know that they won't contain square brackets, you could just use split and then trim off the closing bracket.

– Harry Cutts
Nov 14 '18 at 0:08





What do you know about the values in the brackets? If you know that they won't contain square brackets, you could just use split and then trim off the closing bracket.

– Harry Cutts
Nov 14 '18 at 0:08













Well Jeff, I have seen many regex that helps, but theyreturn the bracket, this is the last I tried: var pattern = /[([^]]*)]/g; var res = pattern.exec(datos[0].title); Another one I tried is: var res = datos[0].title.match(/^.*?[([^]]*)].*?[([^]]*)]/gm); but none do what I need...

– Hexxul
Nov 14 '18 at 0:11






Well Jeff, I have seen many regex that helps, but theyreturn the bracket, this is the last I tried: var pattern = /[([^]]*)]/g; var res = pattern.exec(datos[0].title); Another one I tried is: var res = datos[0].title.match(/^.*?[([^]]*)].*?[([^]]*)]/gm); but none do what I need...

– Hexxul
Nov 14 '18 at 0:11














Put it on the post. Please look at stackoverflow.com/help/how-to-ask

– Abana Clara
Nov 14 '18 at 0:14





Put it on the post. Please look at stackoverflow.com/help/how-to-ask

– Abana Clara
Nov 14 '18 at 0:14




2




2





Possible duplicate of Regular expression to extract text between square brackets

– slider
Nov 14 '18 at 0:16





Possible duplicate of Regular expression to extract text between square brackets

– slider
Nov 14 '18 at 0:16













Abana, I don't know why it is relevant to post code that I've tested and does not do what It is required, but updated my post. I'm trying to find a way "that does it all" a regex that gets anything inside the Square brackets (even white spaces)

– Hexxul
Nov 14 '18 at 0:18





Abana, I don't know why it is relevant to post code that I've tested and does not do what It is required, but updated my post. I'm trying to find a way "that does it all" a regex that gets anything inside the Square brackets (even white spaces)

– Hexxul
Nov 14 '18 at 0:18












5 Answers
5






active

oldest

votes


















2














As @HarryCutts stated, you don't need regex:






var x = "[value1][value2]";
console.log( x.slice(1,-1).split('][') );








share|improve this answer






























    0














    You can try this regex and the brute force way to extract the contents.






    var regex = /[(.*?)]/g;

    var value = "[value1][value2]";

    var matches = value.match(regex);

    var matchedValues = matches.map(match =>
    return match.replace("[", "").replace("]", "");
    ).join(" ");

    console.log(matchedValues.toString())








    share|improve this answer






























      0














      You could just do this:






      var str = "['value1']['value2']";
      var value1 = str.split("]")[0].split("[")[1];
      var value2 = str.split("]")[1].split("[")[1];
      console.log(str);
      console.log(value1);
      console.log(value2);








      share|improve this answer






























        0














        You can easily expand it for more values.






        const string = "[value1][value2]";

        const removeBrackets = (stringWithBrackets) =>
        return stringWithBrackets.split("][").map(s => s = s.replace(/[*]*/g, ""));
        ;

        const [value1, value2] = removeBrackets(string);

        console.log(value1, value2);








        share|improve this answer






























          0














          const getItems = (fullItemsString) => 
          let items = fullItemsString.replace(/[/g, "").split("]");
          items.pop()

          return items;



          Using:



          let items = getItems("[2][34][1]");


          result: [ '2', '', '34', '1' ]






          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%2f53291242%2fhow-can-i-get-the-value-from-a-string-that-has-the-values-inside-a-square-bracke%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            5 Answers
            5






            active

            oldest

            votes








            5 Answers
            5






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            As @HarryCutts stated, you don't need regex:






            var x = "[value1][value2]";
            console.log( x.slice(1,-1).split('][') );








            share|improve this answer



























              2














              As @HarryCutts stated, you don't need regex:






              var x = "[value1][value2]";
              console.log( x.slice(1,-1).split('][') );








              share|improve this answer

























                2












                2








                2







                As @HarryCutts stated, you don't need regex:






                var x = "[value1][value2]";
                console.log( x.slice(1,-1).split('][') );








                share|improve this answer













                As @HarryCutts stated, you don't need regex:






                var x = "[value1][value2]";
                console.log( x.slice(1,-1).split('][') );








                var x = "[value1][value2]";
                console.log( x.slice(1,-1).split('][') );





                var x = "[value1][value2]";
                console.log( x.slice(1,-1).split('][') );






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 '18 at 0:20









                cybersamcybersam

                39.8k43151




                39.8k43151























                    0














                    You can try this regex and the brute force way to extract the contents.






                    var regex = /[(.*?)]/g;

                    var value = "[value1][value2]";

                    var matches = value.match(regex);

                    var matchedValues = matches.map(match =>
                    return match.replace("[", "").replace("]", "");
                    ).join(" ");

                    console.log(matchedValues.toString())








                    share|improve this answer



























                      0














                      You can try this regex and the brute force way to extract the contents.






                      var regex = /[(.*?)]/g;

                      var value = "[value1][value2]";

                      var matches = value.match(regex);

                      var matchedValues = matches.map(match =>
                      return match.replace("[", "").replace("]", "");
                      ).join(" ");

                      console.log(matchedValues.toString())








                      share|improve this answer

























                        0












                        0








                        0







                        You can try this regex and the brute force way to extract the contents.






                        var regex = /[(.*?)]/g;

                        var value = "[value1][value2]";

                        var matches = value.match(regex);

                        var matchedValues = matches.map(match =>
                        return match.replace("[", "").replace("]", "");
                        ).join(" ");

                        console.log(matchedValues.toString())








                        share|improve this answer













                        You can try this regex and the brute force way to extract the contents.






                        var regex = /[(.*?)]/g;

                        var value = "[value1][value2]";

                        var matches = value.match(regex);

                        var matchedValues = matches.map(match =>
                        return match.replace("[", "").replace("]", "");
                        ).join(" ");

                        console.log(matchedValues.toString())








                        var regex = /[(.*?)]/g;

                        var value = "[value1][value2]";

                        var matches = value.match(regex);

                        var matchedValues = matches.map(match =>
                        return match.replace("[", "").replace("]", "");
                        ).join(" ");

                        console.log(matchedValues.toString())





                        var regex = /[(.*?)]/g;

                        var value = "[value1][value2]";

                        var matches = value.match(regex);

                        var matchedValues = matches.map(match =>
                        return match.replace("[", "").replace("]", "");
                        ).join(" ");

                        console.log(matchedValues.toString())






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Nov 14 '18 at 0:20









                        Sushanth --Sushanth --

                        49.9k64784




                        49.9k64784





















                            0














                            You could just do this:






                            var str = "['value1']['value2']";
                            var value1 = str.split("]")[0].split("[")[1];
                            var value2 = str.split("]")[1].split("[")[1];
                            console.log(str);
                            console.log(value1);
                            console.log(value2);








                            share|improve this answer



























                              0














                              You could just do this:






                              var str = "['value1']['value2']";
                              var value1 = str.split("]")[0].split("[")[1];
                              var value2 = str.split("]")[1].split("[")[1];
                              console.log(str);
                              console.log(value1);
                              console.log(value2);








                              share|improve this answer

























                                0












                                0








                                0







                                You could just do this:






                                var str = "['value1']['value2']";
                                var value1 = str.split("]")[0].split("[")[1];
                                var value2 = str.split("]")[1].split("[")[1];
                                console.log(str);
                                console.log(value1);
                                console.log(value2);








                                share|improve this answer













                                You could just do this:






                                var str = "['value1']['value2']";
                                var value1 = str.split("]")[0].split("[")[1];
                                var value2 = str.split("]")[1].split("[")[1];
                                console.log(str);
                                console.log(value1);
                                console.log(value2);








                                var str = "['value1']['value2']";
                                var value1 = str.split("]")[0].split("[")[1];
                                var value2 = str.split("]")[1].split("[")[1];
                                console.log(str);
                                console.log(value1);
                                console.log(value2);





                                var str = "['value1']['value2']";
                                var value1 = str.split("]")[0].split("[")[1];
                                var value2 = str.split("]")[1].split("[")[1];
                                console.log(str);
                                console.log(value1);
                                console.log(value2);






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Nov 14 '18 at 0:21









                                Jack BashfordJack Bashford

                                9,62131540




                                9,62131540





















                                    0














                                    You can easily expand it for more values.






                                    const string = "[value1][value2]";

                                    const removeBrackets = (stringWithBrackets) =>
                                    return stringWithBrackets.split("][").map(s => s = s.replace(/[*]*/g, ""));
                                    ;

                                    const [value1, value2] = removeBrackets(string);

                                    console.log(value1, value2);








                                    share|improve this answer



























                                      0














                                      You can easily expand it for more values.






                                      const string = "[value1][value2]";

                                      const removeBrackets = (stringWithBrackets) =>
                                      return stringWithBrackets.split("][").map(s => s = s.replace(/[*]*/g, ""));
                                      ;

                                      const [value1, value2] = removeBrackets(string);

                                      console.log(value1, value2);








                                      share|improve this answer

























                                        0












                                        0








                                        0







                                        You can easily expand it for more values.






                                        const string = "[value1][value2]";

                                        const removeBrackets = (stringWithBrackets) =>
                                        return stringWithBrackets.split("][").map(s => s = s.replace(/[*]*/g, ""));
                                        ;

                                        const [value1, value2] = removeBrackets(string);

                                        console.log(value1, value2);








                                        share|improve this answer













                                        You can easily expand it for more values.






                                        const string = "[value1][value2]";

                                        const removeBrackets = (stringWithBrackets) =>
                                        return stringWithBrackets.split("][").map(s => s = s.replace(/[*]*/g, ""));
                                        ;

                                        const [value1, value2] = removeBrackets(string);

                                        console.log(value1, value2);








                                        const string = "[value1][value2]";

                                        const removeBrackets = (stringWithBrackets) =>
                                        return stringWithBrackets.split("][").map(s => s = s.replace(/[*]*/g, ""));
                                        ;

                                        const [value1, value2] = removeBrackets(string);

                                        console.log(value1, value2);





                                        const string = "[value1][value2]";

                                        const removeBrackets = (stringWithBrackets) =>
                                        return stringWithBrackets.split("][").map(s => s = s.replace(/[*]*/g, ""));
                                        ;

                                        const [value1, value2] = removeBrackets(string);

                                        console.log(value1, value2);






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Nov 14 '18 at 0:51









                                        amsams

                                        2187




                                        2187





















                                            0














                                            const getItems = (fullItemsString) => 
                                            let items = fullItemsString.replace(/[/g, "").split("]");
                                            items.pop()

                                            return items;



                                            Using:



                                            let items = getItems("[2][34][1]");


                                            result: [ '2', '', '34', '1' ]






                                            share|improve this answer



























                                              0














                                              const getItems = (fullItemsString) => 
                                              let items = fullItemsString.replace(/[/g, "").split("]");
                                              items.pop()

                                              return items;



                                              Using:



                                              let items = getItems("[2][34][1]");


                                              result: [ '2', '', '34', '1' ]






                                              share|improve this answer

























                                                0












                                                0








                                                0







                                                const getItems = (fullItemsString) => 
                                                let items = fullItemsString.replace(/[/g, "").split("]");
                                                items.pop()

                                                return items;



                                                Using:



                                                let items = getItems("[2][34][1]");


                                                result: [ '2', '', '34', '1' ]






                                                share|improve this answer













                                                const getItems = (fullItemsString) => 
                                                let items = fullItemsString.replace(/[/g, "").split("]");
                                                items.pop()

                                                return items;



                                                Using:



                                                let items = getItems("[2][34][1]");


                                                result: [ '2', '', '34', '1' ]







                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Nov 14 '18 at 1:01









                                                Fernando MatosFernando Matos

                                                111




                                                111



























                                                    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%2f53291242%2fhow-can-i-get-the-value-from-a-string-that-has-the-values-inside-a-square-bracke%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

                                                    Use pre created SQLite database for Android project in kotlin

                                                    Darth Vader #20

                                                    Ondo