Match Beginning String Patterns1

Tell us what’s happening:

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.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/regular-expressions/match-beginning-string-patterns

You need to read the instructions carefully.

Use the caret character in a regex to find “Cal” only in the beginning of the string rickyAndCal.

You are trying to match everything except Cal but you need to match only “Cal” using caret character.

That I understand it but I don’t know what code to use

This example in the instruction will help you.

let firstString = "Ricky is first and can be found.";
let firstRegex = /^Ricky/;
firstRegex.test(firstString);
// Returns true

If I out this ^ to my code that tell me it is wrong

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'

I try everything but i don’t remember it because I am and 14 years old

And I don’t can understand this:
Your regex should search for “Cal” with a capital letter.

I don’t understand what code I should use.

The capital letter is C, it needs to be capital ie C instead of c

let rickyAndCal = “Cal and Ricky both like racing.”;
let CalRegex = /and Ricky both like racing/; // Change this line
let result = calRegex.test(rickyAndCal);

That??

Do you see where it says ‘Change this line’? It wants you to change that line to have a regular expression with the caret symbol that looks for 'Cal'

Basically try to copy and understand the example, which had a regular expression with the caret symbol that looks for 'Ricky'

CalRegex
I chenge That?Omg this is hard?

Yes, you change let calRegex = (your code goes here)

Yes it sad me that change this line

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

Well, mine was just an example. It is not necessarily compatible with the solution.

My question is that what pattern should you use. Instead of giving me the whole code, just give me the answer for the fill in the blank

And examine this statement carefully.

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’?

Omg I can’t do this code.:confused:

Well, try it again after a couple of hours later.

1 Like

ok Thank you for helping anyway.