I am coding a validation script; this part checks for valid north American phone #s… (###) ### ####, ##########, ### ### ####, ###-###-####… and such.
I figured it would be simple since I am essentially looking for a string that contains 9 digits, and potentially (, ), - and space. However my regex CRASHES the entire script. I think it is the regex because when I replace the expression with something like “111” the script works ( of course it doesn’t validat anything that is not “111”… but you get my point.
this is what I have as my expression:
var reg = new RegExp("\\(?[0-9]{3}\\)?.[- ]?[0-9]{3}.[- ]?[0-9]{4}$");
it seems pretty basic, so I dont understand why it would be wrong, let alone CRASH the script.
As I said a fresh perspective would be greatly appreciated
i thought the dot = maches a single character… so as to use nothing, space or dash
I can get this to work:
var reg = new RegExp("[0-9]{3}[- ]?[0-9]{3}[- ]?[0-9]{4}$");
I suppose the problem is getting it to understand that “(” and “)” are optional characters in the first part of the string… which I thought could be done with : “\(?” and “\)?”
Unfortunately a dot isn’t nothing. A dot matches a single character, so the character set would then apply to the character after the one matched by the dot.
Yes, that seems right. One potential issue though is if the number you’re testing uses one parenthesis but not the other. It will still match even though it’s not right.
###) ### ####
One way around that is to use the OR operator so that you can specify that it must start with ### or (###)
This works, but it will not pick up the closing ) as mentioned above.
var regEx = /\(?[0-9]{3}\)?[- ]?[0-9]{3}[- ]?[0-9]{4}/gi;
var pNumbers=“505-555-1234 (207) 678-3216 4568904372”;
var getIt=pNumbers.match(regEx);
// show all matches
alert(getIt.join())
You want to match one of these two at the start of the string
either: [0-9]{3}
or: \([0-9]{3}\)
So, you put them in grouping where you use the pipe symbol to specify that you want to match either the first part before the pipe, or the second part after the pipe
([0-9]{3}|\([0-9]{3}\))
That will succeed on ### ### #### and (###) ### ####
and it will fail on ###) ### ####
There is one more case though, (### ### ####
That’s because your regular expression currently can start at any location.
Use ^ at the start to ensure that it starts checking from the start of the string
I was just about to up the file when it occurred to me try an extra “\” before the “(” and “)”… so I used the following and it works:
var reg = new RegExp("^([0-9]{3}|\\\\([0-9]{3}\\\\))[- ]?[0-9]{3}[- ]?[0-9]{4}");
why I have to double escape to get the parenthesis to work is not clear to me tho, but I noticed i had to do it before when attempting to check for wgitespace on another part of the script.
function hasClass(ele,cls) {
return ele.className.match(new RegExp('(\\\\s|^)'+cls+'(\\\\s|$)'));
}
When using the RegExp constructor, that uses a string to define the regular expression. In normal javascript, the backslash is used to escape special characters, such as \r and \
a = ‘this
that’
results in:
“this
that”
So, if you want to ensure that the backslash remains as a backslash in the string, you need to use a double backslash to ensure that the string knows that it must be a backslash
a = ‘this\
that’
results in:
“this
that”
So for the RegExp constructor, you need to double escape the backslash
var reg = new RegExp(“\\(? etcetera”);
When you’re dealing with a regex literal, that is not a string so no escaping of backslashes has to occur.
var reg = /\(? etcetera/;
It is normally better to use the regex literal in your code, and to reserve the complications of the RegExp constructor for when you need to build the regular expression from different strings, which is an uncommon occurrence.