let rickyAndCal = "Cal and Ricky both like racing.";
let calRegex = /^[a-z]/gi; // Change this line
let result = calRegex.test(rickyAndCal);
let arr=rickyAndCal.match(calRegex)
console.log(arr);
the arr should have the value “Cal and Ricky both like racing.”…but is having only “C” .
why?
Because you specified the ^ at the beginning, your current regular expression is only going to match a single letter which at the beginning of the string. The first letter is “C”. What is the actual requirement for the regular expression? You said it should match the entire sentence in rickyAndCal, but what should it not match?
there is a statement in the above link Outside of a character set, the caret is used to search for patterns at the beginning of strings.
Change the regex ohRegex to match only 3 to 6 letter h’s in the word “Oh no”.
i want to know the difference between using
let rickyAndCal = "Cal and Ricky both like racing.";
let calRegex = /^[a-z]al/gi; // Change this line
let calRegex = /^Cal/; // Change this line
let result = calRegex.test(rickyAndCal);
//console.log(result)
let arr=rickyAndCal.match(calRegex)
console.log(arr);
Using the string “Cal and Ricky both like racing.”, they both would have “Cal” in the array, because in the first regex (below), It will match any single letter at the beginning of the string followed immediately by “al”. The “C” is a single letter at the beginning of the string, so “Cal” is matched.
let calRegex = /^[a-z]al/gi; // Change this line
In the second regex (below), it will only match “Cal” if these are the first three letters of the string.
let calRegex = /^Cal/; // Change this line
One notable difference between the two regular expressions is the first one would also match “cal” (lower case “c” because of the i flag). the second regex will not match “cal”, because you do not use the i flag.