\d is not validating other numbers

Tell us what’s happening:
i’m wondering why \d is not validating 1 456 789 4444. if \d{4} is limiting whole number to be 4 digit only.
how can i make it so it only also accept whole number.

Your code so far


function telephoneCheck(str) {
  // Good luck!
  let regexp = /^1?(\W*\d{3}\W*)\1\d{4}$/
  return regexp.test(str);
}

telephoneCheck("555-555-5555");

that regex match will valid for a string like
'1 123 123 5555' ( there are two space between the first 123 and the second )

the usage of \1 should be the question

best is to see how the \1 behave on the following example

/// repeating pattern
/^(\d{3})\s(\d{3})$/.test("123 123") // true
/^(\d{3})\s(\d{3})$/.test("123 456") // true

/// repeating result string using back reference
/^(\d{3})\s\1$/.test("123 123") // true
/^(\d{3})\s\1$/.test("123 456") // false, because "456" != "123"

from the above example shows that \1 will use “the result string that has been matched previously” instead of repeating the pattern.

this is probably the problem that you encountered.

thanks @psyperl for help.