How to console.log results of regex expressions

In the example related to the Regular Expressions: Reuse patterns using capture groups, I was wondering how to print the results of the example so I can see how it worked on Repl or sublime text.

let repeatStr = "regex regex";
let repeatRegex = /(\w+)\s\1/;
let result = repeatRegex.test(repeatStr); // Returns true
let result2 = repeatStr.match(repeatRegex.test(repeatStr)); // Returns ["regex regex", "regex"]
//this value returns false;
console.log(result);
//why does this code return null? How can I fix it?
console.log(result2);

Can anyone please point me in the right direction?

When I run your code, my console shows true for result as indicated in your comment above. I do not get false and should not get false, because it correctly matches a repeat of the first string after the space character.

The variable result to is null and that is correct, because repeatRegex.test(repeatStr) returns a Boolean (true in this case) and not a regular expression which is expected as the argument for the match method used.

1 Like