Help w/RegEx Needed

I need a RegEx pattern which tells me if the first character in a string is a 1 or not. This is going to be used on a telephone number like 15551234567. What I need to do is write a javascript function using RegEx to determine if the first character in the strong (telephone number) is a 1.

Tried all sorts of different snippets from the web, but nothing seems to be working and I am very new at this RegEx things.

Thanks
savetheorcasAsked:
Who is Participating?
 
McOzCommented:
Oops, posted too soon. Change to this to return true or false:
function firstCharacterIsOne(str){
return str.match(/^1/)!=null;
}

Open in new window

0
 
McOzCommented:
Try this: it will return true or false.
function firstCharacterIsOne(str){
return str.match(/^1/);
}

Open in new window

0
 
point_pleasantCommented:
'^1\d{10}'
0
 
point_pleasantCommented:
actually for a US phone number the regex should be (without dashes)
^1[2-9]\d{9}

with dashes

^1[2-9]\d{2}-\d{3}-\d{4}$


0
 
Albert Van HalenAnalyst developerCommented:
No need to use regular expressions (although you can, McOz solution is OK).
You can also use the simpler indexOf method. http://www.w3schools.com/jsref/jsref_IndexOf.asp
function firstCharacterIsOne(str) {
    return str.indexOf("1") == 0;
}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.