Tell us what’s happening:
Hello! So I began this adventure of an exercise… and well regex just are a nice little headache; however, I did solve it! Though, my solution did include 10 if else statements (hahaha), until I checked the basic solution from FCC and see they did it all in one line. I spent some time reading over the code before I implemented it myself to see it complete the exercise. My question is as follows: What is the difference between (), and [], when including the ‘?’ in regular expressions? I am specifically referring to, ^(1\s?)?, and, [\s-]?. I understand the first example checks if there is a “1” or "1 ", in the beginning of the phone number, and the second one checks for a space, or dash, between the digits, but why is it that they both perform a similar action (case 1 OR case 2), but one uses parentheses with two question marks, and one uses brackets with only one question mark? I hope this is clear, I’m really attempting to grasp the world of regex, haha. Thank you!
Your code so far
function telephoneCheck(str) {
var regex = /^(1\s?)?(\(\d{3}\)|\d{3})[\s\-]?\d{3}[\s\-]?\d{4}$/;
return regex.test(str);
};
telephoneCheck("555-555-5555");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/telephone-number-validator
1 555-5555555-
^(1\s?) ? ((\d{3})|\d{3})[\s-]?\d{3}[\s-]?\d{4}$
for example we have number 1234.or 1456 . what we want to map is the first character should be one
so that /^1(\d{3})/ we can use this code . it wont care what are the three followed number after one.
so if i write 1234 it match or 1245 it will match . but if i say 2345 it wont match. so what i try to say is. if ^ is the before any letter or number we can only map renaming number followed by the first character.
so we got a situation we have to match 1234 and 234. what can we do now. Now hero is going to enter into movie. our hero is ‘?’ if i write ^1?(\d{3}) like this , it wont care which is the first character. if one is there (1234) it match . otherwise it can match 1234. the flag s care about space. if i have a sentence ’ i am going’ . if i want to count how many space is there i should use s flag .
so here ^(1\s?) ? ^ flag will check whether first character is one or not ; if one is there it can match or as ^(1\s?) {?} the question mark that i put inside the curly bracket (i just put that question mark inside the curly bracket to make you understand that which flag i am taking about, this flag is called look ahead. i am not sure about the name) . next flag you can see s flag followed by ^(1\s{?})? look ahead or question mark (i put that question mark inside the curly bracket). as i told you before s flag is used for counting space. for example if i want to match 12 or 1 2 ,i can match that as that s flag and s flag followed by look ahead . so now we can map only this one and space if it is there {1 space}555-555-5555 . now we have to match next three number 1 555. so did you notice that we have to match next three number and if ( bracket is there we have to match that also. for that the cod we use is
(\ (\d{3}) | \d{3}) . i will talk about this code. if you want to match a or b you can write a| b right:? the same way .(\ (\d{3}) this code will check bracket is there and \d{3}) this code will map if bracket is not there . now we matched 1 555 or 1555 or 1(555). i forgot to say. this d flag is used for matching number. /\d/ means i can match only first number if i write /\d/g d flag along with g flag i can map all number what if i want to map only specified number of digit. for example 333 i have to match this 333 . but if there is one more 3333 it should not be matched . so i can write /\d{3}/ . if i want to write 4421
the code you have to write is /\d{4} ok so we matted till 1555 . .
. Now next three character
[\s-]?\d{3}[\s-]?
i already mentioned about s flag and look ahead now only one systole left - it is not a flag. it use for mapping 1-2 whether hyphen is there or not
we matched till 1555555
now the last four character .
\d{4}$ see the code . it ends with $ sign . if any regex code end with $ that means it is the last letter or number or symbol to match.
I am sry to say I don’t know whether i can convey everything. my writing skill is a bit bad. please dont mind.
3 Likes