Now I have shaken off yesterday's hangover and am back with you.
I don't know the Robin Nixon book, so can't comment on that, but the Jeremy Keith book is excellent. I think that's a good choice.
I appreciate that, so let me at least explain what the regex does:
The two forward slashes are delimiters which denote that everything between them is a regular expression.
The top hat (or caret ^) matches the position before the first character in the string. For example, applying
The square brackets denote a character class. With a character class, you can tell the regex engine to match only one out of several characters.
For example, if you want to match an a or an e, use
[ae]. In this way you could use
gr[ae]y to match either
In our case
[0-9()+\\s-] denotes one of either: a single digit, left bracket, right bracket, plus, whitespace character (this is the
\\s) or a minus.
The asterisk after the closing square bracket means that the preceding expression should occur zero or more times.
This is useful if the telephone number is not a compulsory field, but when filled in, should be valid.
If you want to make it a compulsory field, swap the asterisk for a plus, which means that it must occur one or more times.
The new reg ex would then be:
Finally, the dollar sign. This is the complement to the caret and indicates the boundary at the end of the string. This is not the last character of the string but the invisible boundary which follows it.
the cat$ will match
watch the cat, but not
the cat eats.
So, I hope that helps.
Good luck learning JavScript.