Javascript regex: test people's name
let result = /^[a-zA-Z ]+$/.test( 'John Doe');
console.log(result);
Throw any symbols you need in the character class. This is why I said be specific about exactly what you want to validate. This regex will not account for accented characters, if you care about that you'd most likely better go with unicode matching.
Try this:
/^(([A-Za-z]+[\-\']?)*([A-Za-z]+)?\s)+([A-Za-z]+[\-\']?)*([A-Za-z]+)?$/
It expects optionally [at least 1 alphabetical character followed by a ' or -] an indefinite number of times. There must be at least one alphabetical character before a required space to ensure we are getting at least the first and last name. This entire pattern is grouped to accept indefinite repetition (for people who like to use all their names, such as John Jacob Jingleheimer Schmidt), but must appear at least once, by means of the + sign right in the middle. Finally, the last name is treated the same way as the other names, but no trailing space is allowed. (Unfortunately this means we are violating DRY a little bit.)
Here is the outcome on several possible pieces of input:
"Jon Doe": true
"Jonathan Taylor Thomas": true
"Julia Louis-Dreyfus": true
"Jean-Paul Sartre": true
"Pat O'Brien": true
"Þór Eldon": false
"Marcus Wells-O'Shaugnessy": true
"Stephen Wells-O'Shaugnessy Marcus": true
"This-Is-A-Crazy-Name Jones": true
"---- --------": false
"'''' ''''''''": false
"'-'- -'-'-'-'": false
"a-'- b'-'-'-'": false
"'-'c -'-'-'-d": false
"e-'f g'-'-'-h": false
"'ij- -klmnop'": false
Note it still doesn't handle Unicode characters, but it could possibly be expanded to include those if needed.
^\s*([A-Za-z]{1,}([\.,] |[-']| ))+[A-Za-z]+\.?\s*$
Similar as @Stephen_Wylie's solution, but shorter (better?).