2

I have a university assignment to write a JS regex for name validation: name can include spaces at the beginning and the end (don't ask anything, it's our teacher demand), also it can include such chars like: -, ' and space (" "). At the moment, my regex looks like that:

var Nameregex = /^( ?)*[A-Z]+((['-])?[a-z]+)*(([ ]?[a-z]*)*)*$/g;

It works almost perfect, but except for one case: one word (words separated from each other with spaces and - symbols) can contain only one ' symbol.

For example, names like John-andrew'andrew'john shouldn't work. But John-andrew'john-andrew'john should work.

1 Answer 1

1

Add a (?!.*'[A-Za-z]+') negative lookahead after ^:

/^(?!.*'[A-Za-z]+')\s*[A-Z]+(?:['-]?[a-z]+)*(?:\s*[a-z]*)*$/

See proof

Explanation

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    '                        '\''
--------------------------------------------------------------------------------
    [A-Za-z]+                any character of: 'A' to 'Z', 'a' to 'z'
                             (1 or more times (matching the most
                             amount possible))
--------------------------------------------------------------------------------
    '                        '\''
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  [A-Z]+                   any character of: 'A' to 'Z' (1 or more
                           times (matching the most amount possible))
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (0 or more times
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    ['-]?                    any character of: ''', '-' (optional
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    [a-z]+                   any character of: 'a' to 'z' (1 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )*                       end of grouping
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (0 or more times
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    [a-z]*                   any character of: 'a' to 'z' (0 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )*                       end of grouping
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! It works almost perfectly. Forgot to mention, that names like John andrew'john andrew'john also should work. So, the final regex looks like: /^(?!.*'[A-Za-z]+') *[A-Z]+(?:[ '-]?[a-z]+)*(?:\s[a-z]*)*$/ There is added space character in the third character set. Thanks again for the help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.