My exercise do more that:
Your regex should search for “Cal” with a capital letter.
Can you help how to do it?
Your code so far
let rickyAndCal = "Cal and Ricky both like racing.";
let calRegex = /and Ricky both like racing/; // Change this line
let result = calRegex.test(rickyAndCal);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.
Klaudia you post questions like this all the time where you just ask for the solution, yet don’t explain what exactly you’ve tried
You’ll find it much easier in the long run to try and figure out yourself why it’s not working, or at the very least explain what you thought would work
Often, when explaining what you’ve already done and explaining why you think it should work you’ll figure it out and/or learn something important
with that being said, lets look at the example:
let firstRegex = /^Ricky/;
The / surrounding it show it’s a regular expression, in this case it’s looking for the string 'Ricky'
The caret symbol ^ makes it look for 'Ricky' only at the start of the string
The challenge you’re at now is to write your own regular expression using a caret symbol (^) to find the string 'Cal'
let rickyAndCal = “Cal and Ricky both like racing.”;
let CalRegex = /and Ricky both like racing/; // Change this line
let result = calRegex.test(rickyAndCal);
Observe the example and the explanation given in the challenge and you can figure it out yourself even without the knowledge of coding.
By checking the explanation, you see
Outside of a character set, the caret is used to search for patterns at the beginning of strings.
Okay, as long as caret is outside of whatever, it means search for patterns at the beginning of strings.
By looking at the example code,
let firstString = "Ricky is first and can be found.";
let firstRegex = /^Ricky/;
firstRegex.test(firstString);
// Returns true
The pattern /^Ricky/ checks if a string begins with Ricky.
To use this pattern, you call some_pattern.test(some_string)
Now the string has changed and you must look for a different pattern
let firstString = "Cal and Ricky both like racing.";
let firstRegex = ___;// Fill in the blank
firstRegex.test(firstString)
The previous example checked whether a string starts with Ricky with /^Ricky/. Now, what should you do to check if the string starts with ‘Cal’?
This challenge assumes that you are already familiar with the concept of variable, calling function, and creating simple Regex pattern. If your understanding of them are fuzzy, you should review previous sections.
let firstString = “Cal and Ricky both like racing.”;
let firstRegex = /Ricky both like racing/;// Fill in the blank
firstRegex.test(firstString)
I try that code above this line but tell me:
// running test
calRegex is not defined
calRegex is not defined
calRegex is not defined
calRegex is not defined
// tests completed