Build A Javascript Calculator

Hello everyone

I am attempting to check e.target.value to see if it contains a specific character, and I am doing this through using Regex.

I setup the characters I am looking for in the handleClick function, and when I do e.target.value.includes(number), it gives me a specific error I am not sure how to solve.

That error is TypeError: Invalid type: first can't be a Regular Expression

I am unsure what first means in this context as I believe I have laid out the Regex correctly and includes() seems to be the standard to find characters in React. Am I doing this correctly? Maybe there is a different function I need to be using? Any advice would be greatly appreciated.

I would also like to apologize in advance if this is an easy question or solution, I am new to React and am still learning.

Link to code: https://codepen.io/Riley-Dickerson-Sprigazalea/pen/GRVgYEM

Hi @Sprigazalea

Try removing the if statement and see what happens?

Happy coding

1 Like

Like this? This still returns the same error

 e.target.value.includes(number) {
      console.log("true")
    }

searchString
A string to be searched for within str. Cannot be a regex. All values that are not regexes are coerced to strings, so omitting it or passing undefined causes includes() to search for the string "undefined", which is rarely what you want.


Edit: Look at test or match if you want to use a regex.

Match ended up fitting with what I needed, thank you!