How to parse the key and get its value in javascript











up vote
1
down vote

favorite












I am opening a URL from javascript. I need to look for the term "colour: x" and then retrieve the value x.



request.get("URL", function (error, res, body)

val = body.indexOf('colour') -> works


which means that web page has the string "colour".



Web page looks like this



size: 8 colour: 1


So, Here I need to retrieve the value of the key 'colour'.










share|improve this question









New contributor




Tamilmani Natarajan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    1
    down vote

    favorite












    I am opening a URL from javascript. I need to look for the term "colour: x" and then retrieve the value x.



    request.get("URL", function (error, res, body)

    val = body.indexOf('colour') -> works


    which means that web page has the string "colour".



    Web page looks like this



    size: 8 colour: 1


    So, Here I need to retrieve the value of the key 'colour'.










    share|improve this question









    New contributor




    Tamilmani Natarajan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I am opening a URL from javascript. I need to look for the term "colour: x" and then retrieve the value x.



      request.get("URL", function (error, res, body)

      val = body.indexOf('colour') -> works


      which means that web page has the string "colour".



      Web page looks like this



      size: 8 colour: 1


      So, Here I need to retrieve the value of the key 'colour'.










      share|improve this question









      New contributor




      Tamilmani Natarajan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I am opening a URL from javascript. I need to look for the term "colour: x" and then retrieve the value x.



      request.get("URL", function (error, res, body)

      val = body.indexOf('colour') -> works


      which means that web page has the string "colour".



      Web page looks like this



      size: 8 colour: 1


      So, Here I need to retrieve the value of the key 'colour'.







      javascript






      share|improve this question









      New contributor




      Tamilmani Natarajan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Tamilmani Natarajan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 15 hours ago









      Manohar Reddy Poreddy

      4,2764144




      4,2764144






      New contributor




      Tamilmani Natarajan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 16 hours ago









      Tamilmani Natarajan

      111




      111




      New contributor




      Tamilmani Natarajan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Tamilmani Natarajan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Tamilmani Natarajan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote













          To search for a pattern in any general text:



          You can use a regular expression if you know how your information is written.



          This regular expression should do the job :



          /bcolour:s+(d+)/


          (the word "colour:" followed by any space, and then by any number of digits (d+).



          It captures the digits, so this will be the value of the first capture group (found[1]) in my example.






          body = `size: 8 colour: 1`

          let regex = /bcolour:s+(d+)/;
          let found = body.match(regex);

          console.log(found[1]);





          In the case there is no match (i.e., no 'colour: xx' in the page), the found result will be null, so you should of course check for it before, for safety.






              body = `size: 8 but unfortunately, no colour here`

          let regex = /bcolour:s+(d+)/;
          let found = body.match(regex);

          //console.log(found[1]); // Uncaught TypeError: Cannot read property '1' of null

          // This snippet below is safe to use :
          if (found) {
          console.log(found[1]);
          } else {
          console.log('not found');
          }








          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: 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
            });


            }
            });






            Tamilmani Natarajan is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53183964%2fhow-to-parse-the-key-and-get-its-value-in-javascript%23new-answer', 'question_page');
            }
            );

            Post as a guest
































            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote













            To search for a pattern in any general text:



            You can use a regular expression if you know how your information is written.



            This regular expression should do the job :



            /bcolour:s+(d+)/


            (the word "colour:" followed by any space, and then by any number of digits (d+).



            It captures the digits, so this will be the value of the first capture group (found[1]) in my example.






            body = `size: 8 colour: 1`

            let regex = /bcolour:s+(d+)/;
            let found = body.match(regex);

            console.log(found[1]);





            In the case there is no match (i.e., no 'colour: xx' in the page), the found result will be null, so you should of course check for it before, for safety.






                body = `size: 8 but unfortunately, no colour here`

            let regex = /bcolour:s+(d+)/;
            let found = body.match(regex);

            //console.log(found[1]); // Uncaught TypeError: Cannot read property '1' of null

            // This snippet below is safe to use :
            if (found) {
            console.log(found[1]);
            } else {
            console.log('not found');
            }








            share|improve this answer



























              up vote
              2
              down vote













              To search for a pattern in any general text:



              You can use a regular expression if you know how your information is written.



              This regular expression should do the job :



              /bcolour:s+(d+)/


              (the word "colour:" followed by any space, and then by any number of digits (d+).



              It captures the digits, so this will be the value of the first capture group (found[1]) in my example.






              body = `size: 8 colour: 1`

              let regex = /bcolour:s+(d+)/;
              let found = body.match(regex);

              console.log(found[1]);





              In the case there is no match (i.e., no 'colour: xx' in the page), the found result will be null, so you should of course check for it before, for safety.






                  body = `size: 8 but unfortunately, no colour here`

              let regex = /bcolour:s+(d+)/;
              let found = body.match(regex);

              //console.log(found[1]); // Uncaught TypeError: Cannot read property '1' of null

              // This snippet below is safe to use :
              if (found) {
              console.log(found[1]);
              } else {
              console.log('not found');
              }








              share|improve this answer

























                up vote
                2
                down vote










                up vote
                2
                down vote









                To search for a pattern in any general text:



                You can use a regular expression if you know how your information is written.



                This regular expression should do the job :



                /bcolour:s+(d+)/


                (the word "colour:" followed by any space, and then by any number of digits (d+).



                It captures the digits, so this will be the value of the first capture group (found[1]) in my example.






                body = `size: 8 colour: 1`

                let regex = /bcolour:s+(d+)/;
                let found = body.match(regex);

                console.log(found[1]);





                In the case there is no match (i.e., no 'colour: xx' in the page), the found result will be null, so you should of course check for it before, for safety.






                    body = `size: 8 but unfortunately, no colour here`

                let regex = /bcolour:s+(d+)/;
                let found = body.match(regex);

                //console.log(found[1]); // Uncaught TypeError: Cannot read property '1' of null

                // This snippet below is safe to use :
                if (found) {
                console.log(found[1]);
                } else {
                console.log('not found');
                }








                share|improve this answer














                To search for a pattern in any general text:



                You can use a regular expression if you know how your information is written.



                This regular expression should do the job :



                /bcolour:s+(d+)/


                (the word "colour:" followed by any space, and then by any number of digits (d+).



                It captures the digits, so this will be the value of the first capture group (found[1]) in my example.






                body = `size: 8 colour: 1`

                let regex = /bcolour:s+(d+)/;
                let found = body.match(regex);

                console.log(found[1]);





                In the case there is no match (i.e., no 'colour: xx' in the page), the found result will be null, so you should of course check for it before, for safety.






                    body = `size: 8 but unfortunately, no colour here`

                let regex = /bcolour:s+(d+)/;
                let found = body.match(regex);

                //console.log(found[1]); // Uncaught TypeError: Cannot read property '1' of null

                // This snippet below is safe to use :
                if (found) {
                console.log(found[1]);
                } else {
                console.log('not found');
                }








                body = `size: 8 colour: 1`

                let regex = /bcolour:s+(d+)/;
                let found = body.match(regex);

                console.log(found[1]);





                body = `size: 8 colour: 1`

                let regex = /bcolour:s+(d+)/;
                let found = body.match(regex);

                console.log(found[1]);





                    body = `size: 8 but unfortunately, no colour here`

                let regex = /bcolour:s+(d+)/;
                let found = body.match(regex);

                //console.log(found[1]); // Uncaught TypeError: Cannot read property '1' of null

                // This snippet below is safe to use :
                if (found) {
                console.log(found[1]);
                } else {
                console.log('not found');
                }





                    body = `size: 8 but unfortunately, no colour here`

                let regex = /bcolour:s+(d+)/;
                let found = body.match(regex);

                //console.log(found[1]); // Uncaught TypeError: Cannot read property '1' of null

                // This snippet below is safe to use :
                if (found) {
                console.log(found[1]);
                } else {
                console.log('not found');
                }






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 12 hours ago

























                answered 16 hours ago









                Pac0

                7,16722443




                7,16722443






















                    Tamilmani Natarajan is a new contributor. Be nice, and check out our Code of Conduct.










                     

                    draft saved


                    draft discarded


















                    Tamilmani Natarajan is a new contributor. Be nice, and check out our Code of Conduct.













                    Tamilmani Natarajan is a new contributor. Be nice, and check out our Code of Conduct.












                    Tamilmani Natarajan is a new contributor. Be nice, and check out our Code of Conduct.















                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53183964%2fhow-to-parse-the-key-and-get-its-value-in-javascript%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest




















































































                    Popular posts from this blog

                    横浜市

                    Rostock

                    Europa