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.