Using the Test() Method

Tell us what’s happening:
As this challenge description, the test() method will returns true or false if they find or not, respectively.
Look at this code, myRegex has “.” value. But it show me true, where myString variable has no “.” string.
It should be return false. Am I right or not???

Your code so far


let myString = "Hello, World!";
let myRegex = /./;
let result = myRegex.test(myString); // Change this line
console.log(result);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.

/./ matches everything, instead you can use character classes like /[.]/ to just match that character,

reference: https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/regular-expressions/match-single-character-with-multiple-possibilities

1 Like

. is a special regex character. If you want its literal meaning you can either use

[.] or \.

let myRegex = /\./;
let myRegex = /[.]/;
1 Like