1

I have been scouring the internet for answers to this regex question and I have come quite close to getting it but I think I am missing one or two more groups to prevent the special characters at the start and end of the string.

It is for use in angularJS and the full specification is a string of letters that cannot be longer than 20 characters, will only allow -,' and space as the special characters but - and ' cannot be consecutively used and cannot be used at the start or end of the string.

Below is the regex I have at the moment:

/(?!.*?[ '-]{2})[A-Za-z '-]{1,20}$/

It prevents any special characters that are not -' or space and allows only 1 of -' to be used consecutively, However it still allows -and ' at the start and end of the string. The regex101 link displays the regex working.

  • -John Doe
  • John Doe-
  • 'John Doe
  • John Doe'

The above 4 need to be flagged as invalid matches but I am unable to figure out how to do that along with all the other expressions that are currently in the regex.

Gal Dreiman
  • 3,969
  • 2
  • 21
  • 40
P.Doohan
  • 45
  • 1
  • 6

1 Answers1

1

You may re-write the pattern to only match 1 non-word char in between lettere, and restrict the length of the regex with a positive lookahead at the start:

/^(?=.{1,20}$)[A-Za-z]+(?:[ '-][A-Za-z]+)?$/

See the regex demo

NOTE: If you use maxlength/minlength you can even get rid of the length restriction lookahead.

Details

  • ^ - start of string
  • (?=.{1,20}$) - the length of the whole string must be from 1 to 20 chars
  • [A-Za-z]+ - 1+ ASCII letters
  • (?:[ '-][A-Za-z]+)? - 1 optional sequence of:
    • [ '-] - 1 space/apostrophe/hyphen
    • [A-Za-z]+ - 1 or more ASCII letters
  • $ - end of string.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • That is excellent, thank you. Could you give me a breakdown of the captured groups, positive look ahead and the non capturing group if you have the time to? Could do with understanding them a bit better. – P.Doohan Nov 09 '17 at 09:46
  • @P.Doohan There is no [capturing group](https://www.regular-expressions.info/brackets.html) here. `(?=.{1,20}$)` is a [positive lookahead](https://www.regular-expressions.info/lookaround.html) where `.` matches any char but a line break char, `{1,20}` is a [limiting quantifier](https://www.regular-expressions.info/repeat.html#limit) matching 1 to 20 consecutive occurrences of the quantified subpattern and `$` matches the end of string position. The rest is explained, more about [non-capturing groups can be found here](https://stackoverflow.com/questions/3512471). – Wiktor Stribiżew Nov 09 '17 at 10:05