Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello there,

SO I am new to JavaScript and I am trying to wrap my hands around Regular Expressions. Essentially what I am trying to do is, making user to type a sentence and check if that statement contains the word 'hello'. And I am not able to do so. If anyone could please help me out with the same. I have given my code below.

What I have tried:

JavaScript
var a = /hello/;

var b = parseInt(prompt("Type your sentence"));

  if(a.test(b)){

    alert("yes");
}
Posted
Updated 5-Apr-18 1:51am

1 solution

Read the documentation: RegExp - JavaScript | MDN[^] and RegExp.prototype.test() - JavaScript | MDN[^]. There you can find examples which you should try out first to become familiar with regular expressions and the syntax.

After doing so you should know that you need two variables:
  1. A regular expression (your var a)
  2. A string to be tested (your var b)
Observe that the second must be a string. But you are using parseInt() - JavaScript | MDN[^] which returns an integer. So just use
JavaScript
var b = prompt("Type your sentence");
 
Share this answer
 
Comments
Member 13748876 5-Apr-18 7:56am    
Thanks a million mate!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900