Regular Expressions - Match Beginning String Patterns

Tell us what’s happening:
Describe your issue in detail here.
Hm… this is supposed to test the beginning of a string pattern.
I was curious about this so I went back to the original lesson on carets titled, " Match Single Characters Not Specified". I changed that code to the following code and expected “false” because what I tested for was NOT at the beginning of the string pattern. It returned “true”. How!? When it should return “false” since it is not at the beginning.

My code for the current lesson

let rickyAndCal = "Cal and Ricky both like racing.";
let calRegex = /^Cal/; // Change this line
let result = calRegex.test(rickyAndCal); console.log(result);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Challenge: Regular Expressions - Match Beginning String Patterns

Link to the challenge:

My code attempting to test this from the first caret lesson

let quoteSample = "3 blind mice.";
let myRegex = /[^blind]/; // Change this line
let result = myRegex.test(quoteSample); console.log(result);// Change this line

The caret (^) is an overloaded symbol in regular expressions. It means different things depending on where it appears in a pattern. When it inside square brackets ([...]) it turns the group into a non-match group. So [^bind]means “Any character that is not ‘b’, ‘i’, ‘n’, or ‘d’.” Outside of a group, ^ is a character representing the start of a line. So ^Cal means “At the start of a line, the character ‘C’ followed by ‘a’ followed by ‘l’.”

1 Like

Thank you! The little things I miss (the brackets in this case). Your explanation added the nuances that will also help me in the future.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.