Spaces
- Will only be allowed within the alpha text
- Multiple spaces will be allowed within the field but cannot be consecutive
Dashes
- Will only be allowed within the alpha text (boyd-lawson" or "boyd – lawson" is ok, but "boyd-" and "boyd " and " "-boyd" is not)
- Multiple dashes will be allowed within the field but cannot be consecutive
Periods
- Cannot be the first character in the field
- Multiple periods will be allowed within the field but cannot be consecutive
Apostrophes
- Multiple apostrophes will be allowed within the field but cannot be consecutive
I tried using the below expression
/^(?!.*(?:ss|-{2,}|.{2,}|'{2,}))(?=[A-Za-zs.-']+$)(?!.*(?:^.))/g
2
Answers
Can you try this
Here is a regular expression which attempts to refactor your requirements into one single definition.
To permit a single-character field, the expression after the first character is optional.
We separately have to prohibit two adjacent apostrophes at the beginning or end using lookarounds.
You might want to restrict
[^- .]
and[^- .']
further if you specifically require these characters to be strictly alphanumeric. For example, the above formulation will permit two adjacent commas or circumflexes; maybe you want to require actual text, or add some other further constraints.