React calculator: check if any value exists in string











up vote
0
down vote

favorite












Is there anyway I could check if more than one value exists in a string? My idea is to have only two conditions:
a) if there´s no decimal, add it;
b) if there´s an operator (+-*/) followed by a number from 0 to 9, add it.
The decimal gets added only when one of those two conditions are met. What string method could I use?



handleDecimal(evt){
const result = evt.target.value;
let value = this.state.value;
if (value.indexOf('.') === -1){
this.setState(prevState => ({
value: this.state.value + result
}))};
}









share|improve this question






















  • What exactly do you mean, "add it"? Do you mean add some numbers together, or insert a decimal point (at what position?), or what? Can you give a couple of examples?
    – CertainPerformance
    Nov 11 at 7:03












  • In the current handle event, if the decimal isn´t there, the decimal gets added. I want something similar to that, but the condition should be something like "set the state with a decimal only if the decimal doesn´t exist already OR if there´s an operator followed by a number". For instance: if the string is "2322399838", you can add a decimal to it (just one). If, after that, there´s a "+2293998" you can add another decimal and so on. Hopefully I´m being clear.
    – Hernan Ariel
    Nov 11 at 7:07

















up vote
0
down vote

favorite












Is there anyway I could check if more than one value exists in a string? My idea is to have only two conditions:
a) if there´s no decimal, add it;
b) if there´s an operator (+-*/) followed by a number from 0 to 9, add it.
The decimal gets added only when one of those two conditions are met. What string method could I use?



handleDecimal(evt){
const result = evt.target.value;
let value = this.state.value;
if (value.indexOf('.') === -1){
this.setState(prevState => ({
value: this.state.value + result
}))};
}









share|improve this question






















  • What exactly do you mean, "add it"? Do you mean add some numbers together, or insert a decimal point (at what position?), or what? Can you give a couple of examples?
    – CertainPerformance
    Nov 11 at 7:03












  • In the current handle event, if the decimal isn´t there, the decimal gets added. I want something similar to that, but the condition should be something like "set the state with a decimal only if the decimal doesn´t exist already OR if there´s an operator followed by a number". For instance: if the string is "2322399838", you can add a decimal to it (just one). If, after that, there´s a "+2293998" you can add another decimal and so on. Hopefully I´m being clear.
    – Hernan Ariel
    Nov 11 at 7:07















up vote
0
down vote

favorite









up vote
0
down vote

favorite











Is there anyway I could check if more than one value exists in a string? My idea is to have only two conditions:
a) if there´s no decimal, add it;
b) if there´s an operator (+-*/) followed by a number from 0 to 9, add it.
The decimal gets added only when one of those two conditions are met. What string method could I use?



handleDecimal(evt){
const result = evt.target.value;
let value = this.state.value;
if (value.indexOf('.') === -1){
this.setState(prevState => ({
value: this.state.value + result
}))};
}









share|improve this question













Is there anyway I could check if more than one value exists in a string? My idea is to have only two conditions:
a) if there´s no decimal, add it;
b) if there´s an operator (+-*/) followed by a number from 0 to 9, add it.
The decimal gets added only when one of those two conditions are met. What string method could I use?



handleDecimal(evt){
const result = evt.target.value;
let value = this.state.value;
if (value.indexOf('.') === -1){
this.setState(prevState => ({
value: this.state.value + result
}))};
}






javascript reactjs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 7:01









Hernan Ariel

13216




13216












  • What exactly do you mean, "add it"? Do you mean add some numbers together, or insert a decimal point (at what position?), or what? Can you give a couple of examples?
    – CertainPerformance
    Nov 11 at 7:03












  • In the current handle event, if the decimal isn´t there, the decimal gets added. I want something similar to that, but the condition should be something like "set the state with a decimal only if the decimal doesn´t exist already OR if there´s an operator followed by a number". For instance: if the string is "2322399838", you can add a decimal to it (just one). If, after that, there´s a "+2293998" you can add another decimal and so on. Hopefully I´m being clear.
    – Hernan Ariel
    Nov 11 at 7:07




















  • What exactly do you mean, "add it"? Do you mean add some numbers together, or insert a decimal point (at what position?), or what? Can you give a couple of examples?
    – CertainPerformance
    Nov 11 at 7:03












  • In the current handle event, if the decimal isn´t there, the decimal gets added. I want something similar to that, but the condition should be something like "set the state with a decimal only if the decimal doesn´t exist already OR if there´s an operator followed by a number". For instance: if the string is "2322399838", you can add a decimal to it (just one). If, after that, there´s a "+2293998" you can add another decimal and so on. Hopefully I´m being clear.
    – Hernan Ariel
    Nov 11 at 7:07


















What exactly do you mean, "add it"? Do you mean add some numbers together, or insert a decimal point (at what position?), or what? Can you give a couple of examples?
– CertainPerformance
Nov 11 at 7:03






What exactly do you mean, "add it"? Do you mean add some numbers together, or insert a decimal point (at what position?), or what? Can you give a couple of examples?
– CertainPerformance
Nov 11 at 7:03














In the current handle event, if the decimal isn´t there, the decimal gets added. I want something similar to that, but the condition should be something like "set the state with a decimal only if the decimal doesn´t exist already OR if there´s an operator followed by a number". For instance: if the string is "2322399838", you can add a decimal to it (just one). If, after that, there´s a "+2293998" you can add another decimal and so on. Hopefully I´m being clear.
– Hernan Ariel
Nov 11 at 7:07






In the current handle event, if the decimal isn´t there, the decimal gets added. I want something similar to that, but the condition should be something like "set the state with a decimal only if the decimal doesn´t exist already OR if there´s an operator followed by a number". For instance: if the string is "2322399838", you can add a decimal to it (just one). If, after that, there´s a "+2293998" you can add another decimal and so on. Hopefully I´m being clear.
– Hernan Ariel
Nov 11 at 7:07














1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










You can use a regular expression and String.prototype.search() to accomplish this.



The RegEx /([-+*/]d)/ will match an operator followed by any digit.



If you want to add a decimal even when there exists a decimal, but there is also an operator followed by a number:



if (value.indexOf === '-1' || value.search(/([-+*/]d)/) > -1) { //add decimal }


If you want to have only one decimal at most in the final string:



if (value.indexOf === '-1') { 
//add decimal
} else if (value.search(/([-+*/]d)/) > -1) {
//add decimal
}





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


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246546%2freact-calculator-check-if-any-value-exists-in-string%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    You can use a regular expression and String.prototype.search() to accomplish this.



    The RegEx /([-+*/]d)/ will match an operator followed by any digit.



    If you want to add a decimal even when there exists a decimal, but there is also an operator followed by a number:



    if (value.indexOf === '-1' || value.search(/([-+*/]d)/) > -1) { //add decimal }


    If you want to have only one decimal at most in the final string:



    if (value.indexOf === '-1') { 
    //add decimal
    } else if (value.search(/([-+*/]d)/) > -1) {
    //add decimal
    }





    share|improve this answer



























      up vote
      1
      down vote



      accepted










      You can use a regular expression and String.prototype.search() to accomplish this.



      The RegEx /([-+*/]d)/ will match an operator followed by any digit.



      If you want to add a decimal even when there exists a decimal, but there is also an operator followed by a number:



      if (value.indexOf === '-1' || value.search(/([-+*/]d)/) > -1) { //add decimal }


      If you want to have only one decimal at most in the final string:



      if (value.indexOf === '-1') { 
      //add decimal
      } else if (value.search(/([-+*/]d)/) > -1) {
      //add decimal
      }





      share|improve this answer

























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        You can use a regular expression and String.prototype.search() to accomplish this.



        The RegEx /([-+*/]d)/ will match an operator followed by any digit.



        If you want to add a decimal even when there exists a decimal, but there is also an operator followed by a number:



        if (value.indexOf === '-1' || value.search(/([-+*/]d)/) > -1) { //add decimal }


        If you want to have only one decimal at most in the final string:



        if (value.indexOf === '-1') { 
        //add decimal
        } else if (value.search(/([-+*/]d)/) > -1) {
        //add decimal
        }





        share|improve this answer














        You can use a regular expression and String.prototype.search() to accomplish this.



        The RegEx /([-+*/]d)/ will match an operator followed by any digit.



        If you want to add a decimal even when there exists a decimal, but there is also an operator followed by a number:



        if (value.indexOf === '-1' || value.search(/([-+*/]d)/) > -1) { //add decimal }


        If you want to have only one decimal at most in the final string:



        if (value.indexOf === '-1') { 
        //add decimal
        } else if (value.search(/([-+*/]d)/) > -1) {
        //add decimal
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 11 at 14:35

























        answered Nov 11 at 14:25









        Syed Rashid

        262




        262






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246546%2freact-calculator-check-if-any-value-exists-in-string%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

            Bressuire

            Vorschmack

            Quarantine