skip to Main Content

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


  1. Can you try this

    const pattern = /^(?!.*--)(?!.*..)(?!.*[^ws]s+)(?!.*s[^ws])(?!.*.s)(?!.*s.)(?!.*'s)(?!.*s')(?!.*[^ws-]s+)(?!.*s[^ws-])(?!.*-s)(?!.*s-)[A-Za-z0-9s.-]*[A-Za-z0-9]$/;
    
    Login or Signup to reply.
  2. Here is a regular expression which attempts to refactor your requirements into one single definition.

    • The first character cannot be dash, space, or period.
    • Within the text, a character can be any non-dash, non-space, non-period, non-apostrophe character, or a dash, space, period, or apostrophe if it is not immediately followed by another.
    • The last character cannot be dash, space, period, or apostrophe.

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search