A caret (^
) is another metacharacter. It matches characters at the start of the string. This is helpful when we need to match multiple strings that start with similar characters.
All the given strings begin from 'It', and hence are successfully matched with the given regular expression '^It'.
xxxxxxxxxx
14
// Adding a function to wrap the existing code
function findMatch() {
var s1 = 'It is rainy.';
var s2 = 'It is cloudy.';
var s3 = 'It is sunny.';
if (/^It/.test(s1) && /^It/.test(s2) && /^It/.test(s3)) {
console.log("Found a match!");
} else {
console.log("Did not find a match.");
}
}
// Driver code to execute the function
findMatch();
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment