Tell us what’s happening:
Describe your issue in detail here.
Your code so far
let extractStr = "Extract the word 'coding' from this string.";
let codingRegex = /coding/; // Change this line
let result = extractStr.match(codingRegex); // Change this line
Your browser information:
User Agent is:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36
Challenge: Extract Matches
Link to the challenge:
Previous challenges were using
regex.test(string) which checks to see if the regex matches with anything within the string, and returns a boolean (true or false) if it matches. It lets you know if there is a match.
This challenge introduces the
string.match(regex) function, which doesn’t return a yes or no if it matched, but instead returns an array of actual matches.
let myString = "I have a cat";
let myRegex = /cat/;
console.log (myRegex.test(myString)); // output should be 'true'
console.log (myString.match(myRegex)); // output should be ["cat"] (an array)
Not a very useful distinction for a simple string search, but useful in more complex scenarios.
You could say the
test method answers the question, “can this regex be found in the string?”, while
match would correspond with “show me the appearance of the regex in the string”.
test returns a single boolean, while
match can contain lot of extra data, which can be useful in various cases. Its important to remember, as been pointed out above, that
test is a regex method, meaning you attach it to a regex and it requires a string as argument, while
match is a string method, it is attached to strings(and required regex as argument).
Appreciate the clarification