Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i learn test regexp,I want it if the value is correct show "learning javascript" but if the value is wrong then show "learning"
the value is always false
any solution?

What I have tried:

this code
JavaScript
const panggilRegexp = (value) => {
  var data1 =/learning+\s+html+/g.test(data1)
  if (data1==true) {
    console.log("learning javascript");
  }else {
  console.log("learning");
  }
}
panggilRegexp("learning javascript")
Posted
Updated 5-Oct-18 5:40am

1 solution

You're running the test against the wrong variable. In the below:

JavaScript
var data1 = /learning+\s+html+/g.test(data1);

You're calling .test(data1) which is testing against a variable which doesn't exist yet. You need to pass the value variable into this instead:

JavaScript
var data1 = /learning+\s+html+/g.test(value);

The regular expression might not be correct too, but I'm sure you will work this out. There are online regular expression evaluators[^] which can help with building the right expression.
 
Share this answer
 

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