Regular Expression: Person's Name

Hello,
I am trying to write a regular expression that meets the following requirements:
1) The person must enter their first and last name (Keeping in mind some people's last name can be "Del La Hurst")
2) The person may use the literal characters . , '   (e.g, John O'Neal)
3) The person may not use excessive spaces. (e.g., an invalid entry would be "John Doe           ")
4) Pretty much anyone's name, would be accepted. Even something like "John O'Neal, jr."

I have tried the three RegExp objects below (I cannot use patterns), but they do not seem to work. What is wrong with the below regular expressions, and does anyone know of a regular expression that would match my requirements?

^(\w{2,})((\s{1})[,.'\w{2,}])+$

^(\w{2,})((\s{1})[,.'\w{2,}]+)$

^(\w{2,})(\s{1}[,.'\w{2,}]+)$

I've set this to 400 points as this is urgent. Thanks!
LVL 1
damijimAsked:
Who is Participating?
 
ZvonkoSystems architectCommented:
How about this>

<script>

var nameExp = /^\w{2,}(\s(\w\')?\w{2,}(,\s\w{2,}\.?)?)+$/;

var names = ["Del La Hurst","John O'Neal","John Doe           ", "John O'Neal, jr."];
for(var i=0;i<names.length;i++){
  alert(names[i]+":\n\t"+nameExp.test(names[i]));
}
</script>


0
 
damijimAuthor Commented:
Great, thanks! :)
0
 
ZvonkoSystems architectCommented:
When great, why then grade B?
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
damijimAuthor Commented:
"What is wrong with the below regular expressions"

Because I still don't understand what was wrong with my reg ex's...
0
 
ZvonkoSystems architectCommented:
And because YOU do not understand and YOU do not ask, therefore I get a B???
0
 
ZvonkoSystems architectCommented:
Or do you like your three expressions explained?
0
 
damijimAuthor Commented:
I was also asking "What is wrong with the below regular expressions" that I had written. They worked for most of the situations I provided, but there was always a quirk.

I do understand why yours works.
0
 
ZvonkoSystems architectCommented:
OK, then you need no more help?
0
 
ZvonkoSystems architectCommented:
Thanks.

Here the explanation was went wrong in your expressions.
You defined a Set betwean square braces.
In a Set betwean [ and ] are all single characters treated as set memebers.
That mean, this Set definition: [,.'\w{2,}]
Contains this single characters:  } { , . '  2 \w  
The order of characters does not matter in a Set.
0
 
damijimAuthor Commented:
Thank you & Thanks GranMod...

Okay, just to make sure I got this clear,

In a reg ex of [abc]+
'abc' = true
'cba' = true
'ab' = false

but if I did [abc]{1} then only a,b, or c by itself would be true.


and in (abc)+
abc = 'true'
ab = 'true'
cba = 'true'

but if I did [abc]{1} then only 'abc'(in any order) by itself would be true.

Right?
0
 
ZvonkoSystems architectCommented:
For this: [abc]+
Are following true:
c
aaaaa
abbb
abc

For this: (abc)+
Are following true:
abc
abcabc
abcabcabc

Here a test page:
http://www.regular-expressions.info/javascriptexample.html


0
 
damijimAuthor Commented:
awesome, thanks! Sorry about the issue earlier!
0
 
ZvonkoSystems architectCommented:
No problem at all.
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.