Help me analyse the code for a formatted phone number in Javascript

Help me analyse the following code for a formatted phone number.The code is as follows:

function validate(phoneNumber) {
 return phoneNumber.match(/^\d{3}-?\d{4}$/);
}

what issue are you having with this code?

The code is supposed to accepted the number(as a string) should
have seven digits, 0 through 9, with
an optional dash in the middle, but i don’t understand the line:
return phoneNumber.match(/^\d{3}-?\d{4}$/);

image

this is the explanation from https://regex101.com/

1 Like