I am making a Regular expression in Javascript to process last names.
I have been asked to be valid:
- One or more surnames
- alphabetic characters (with accents), ñ and apostrophe.
- the apostrophe can go at the end of a surname (Di' Stefan) or in the middle (O'Brian) but not at the beginning (
'OBrian,O 'Brian) - two surnames can be separated by hyphens (Menendez Martin-Vega)
EDIT
- The last surname cannot end in an apostrophe or hyphen either. Ex: (Menendez- , Fernandez', Martin - , ...)
I made a regex that works in the online debugger.
([a-z\u00E0-\u00FC]+[']?[a-z\u00E0-\u00FC]?[\s-]*)+$
But implementing it in javascript doesn't fail on apostrophes at the beginning:
var str = "'Obrian o";
var patt = new RegExp(/([a-z\u00E0-\u00FC]+[']?[a-z\u00E0-\u00FC]?[\s-]*)+$/i );
console.log(patt.test(str));
What is the difference between the debugger and my function? How can I achieve the required points?
You were simply missing a
^
at the beginning of the pattern to tell it to start checking from it. Otherwise, it will start at any position to perform the checks, so the pattern is perfectly fulfilled just after the apostrophe, which is not what you want:Now the pattern is more complex and separated into two parts:
An improvement, to improve debugging and make any modification easier:
Another method in which, to simplify the regular expression, we check each of the last names in parts. Note the detail that, in case of only having a last name, the loop that checks the pattern
pat_ini
will never be executed.