• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 318
  • Last Modified:

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!
0
damijim
Asked:
damijim
  • 8
  • 5
1 Solution
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 8
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now