Regex help

I could use a fresh set of eyes.

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

What are those dots doing in the expression?

Also, 3 + 3 + 4 = 10, not 9 …

my bad I meant 10.

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.

It’s good to see that you got it working as expected.
A good place to test them is at http://www.regular-expressions.info/javascriptexample.html

got that. :slight_smile:

how would i select and optional “(” … like i said I thought it was “\(?”

Yes, that seems right. What issues are you facing with that?

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

^([0-9]{3}|\([0-9]{3}\))

Using that with the rest of the regex gives you:

/^([0-9]{3}|\([0-9]{3}\))[- ]?[0-9]{3}[- ]?[0-9]{4}/gi

thanks Paul… I am using this, as you suggested:
var reg = new RegExp(“^([0-9]{3}|\([0-9]{3}\))[- ]?[0-9]{3}[- ]?[0-9]{4}”);

but for some reason I am still getting a FALSE of any numbers with (###) at the beginning…

That’s odd, it seems to work for me.

Can you link through to a test page that allows us to experience the problem?

Paul, thanks for your help.

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|$)'));
}

In case you are curious … the page is here

Thanks again, Paul , for all your help.

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.