I am attempting to change the color of a text element when specific variations of the word "hello" are entered into an input field. While this works with a regular string comparison, it fails when using a regular expression.
<input id="input" type="input" value="hello"/>
<span id="result">Hello</span>
<script>
var input = document.getElementById('input');
var result = document.getElementById('result');
function changeToGreen(){
result.style.color = 'green';
}
function changeToRed(){
result.style.color = 'red';
}
input.addEventListener('keyup', function handleChange(){
var inputValue = input.value;
if(inputValue == /hello/i ){ //this section does not work
changeToGreen();
}
if(inputValue != /hello/i ){ //nor does this one
changeToRed();
}});
</script>
</body>
</html>
Using if(inputValue == "hello" )
produces the desired result, but if(inputValue == /hello/i )
does not.