Search string for numbers

I have a javascript chat bot where a person can enter any question they like in the input field and hope to get an exact answer. I can do this, but I know that all this is wrong, because I do not know in what position the number will appear in the sentence. If a person enters exactly:

which square root of 5 works fine.

If he is type in such things, it is not.

what is the square root of 5

the square root of 5 is that

Did you know that the square root of 5

etc.

I need to determine where the number appears in the sentence, and then perform the calculation from there. Please note that the line below is part of a larger working chat. In the line below, I'm just trying to answer any question with a square root no matter where the number appears in the sentence. I also know that there are many pitfalls with an open input field, where a person can enter anything, for example, spelling errors, etc. This is just for fun, not a serious scientific project. :)

if(
    (word[0]=="what's") &&
    (word[1]=="the") &&
    (word[2]=="square") &&
    (word[3]=="root") &&
    (word [4]=="of") &&
    (input.search(/\d{1,10}/)!=-1) &&
    (num_of_words==6)
){        
    var root= word[5];
    if(root<0){ 
        document.result.result.value = "The square root of a negative number is not possible.";
    }else{
         word[5] = Math.sqrt(root);
         word[5] = Math.round(word[5]*100)/100 
         document.result.result.value = "The square root of "+ root +" is "+ word[5] +"."; 
    }
    return true;
}

, , "If statemments" - . "" " " " ", , " ". , , . , . , . .

+5
2

, .

"What is the sqrt of 5?".match(/\d+/g);
["5"]

, , . , " 100 100?"?

"Is the square root of 100 10?".match(/\d+/g);
["100", "10"]

, script.

+2

, , , , , . JavaScript https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions.

, , , , , . , pi e.

nums = someString./\d+|pi|e/gi

, , , , " ", "" "". , . ,

What is 5 plus 3 minus 8?

,

What is 5 minus 3 plus 8?

.

, , , , , . , , , , , .

, , , , , . . .

+1

All Articles