Why is it necessary to use ^ and $ to look for a string that contains all strings I am looking for?

I am trying to simplify this solution and I still do not understand why, even after removing spaces I need to define the limits through ^ and $ if repeatNum contain all the groups, and not any extra string, that I want to include in reRegex.

Describe your issue in detail here.

Your code so far


let repeatNum = "42 42 42";
let reRegex = /^(\d+) \1 \1$/; // Change this line
let result = reRegex.test(repeatNum);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0

Challenge: Reuse Patterns Using Capture Groups

Link to the challenge:

It tests true if the string contains a substring that matches the pattern - if you remove what matches start of string and end of string, you remove the thing that make sure that it must match all the string

To illustrate ilenia’s point:

const regex1 = /^(\d+) \1 \1$/

console.log(regex1.test('42 42 42'))
// true

console.log(regex1.test('apple 42 42 42 jack'))
// false

const regex2 = /(\d+) \1 \1/

console.log(regex2.test('42 42 42'))
// true

console.log(regex2.test('apple 42 42 42 jack'))
// true